blob: 7120d912905566a5cf59a93606783939258e21cb [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
Chris Lattnerebb89b42009-09-27 21:16:52 +000033AsmParser::AsmParser(SourceMgr &_SM, MCContext &_Ctx, MCStreamer &_Out,
34 const MCAsmInfo &_MAI)
Sean Callananfd0b0282010-01-21 00:19:58 +000035 : Lexer(_MAI), Ctx(_Ctx), Out(_Out), SrcMgr(_SM), TargetParser(0),
Chris Lattnerf0559e42010-04-08 20:30:37 +000036 CurBuffer(0) {
Sean Callananfd0b0282010-01-21 00:19:58 +000037 Lexer.setBuffer(SrcMgr.getMemoryBuffer(CurBuffer));
38
Chris Lattnerebb89b42009-09-27 21:16:52 +000039 // Debugging directives.
40 AddDirectiveHandler(".file", &AsmParser::ParseDirectiveFile);
41 AddDirectiveHandler(".line", &AsmParser::ParseDirectiveLine);
42 AddDirectiveHandler(".loc", &AsmParser::ParseDirectiveLoc);
43}
44
45
46
Daniel Dunbar7c0a3342009-08-26 22:49:51 +000047AsmParser::~AsmParser() {
Daniel Dunbar7c0a3342009-08-26 22:49:51 +000048}
49
Daniel Dunbarf9507ff2009-07-27 23:20:52 +000050void AsmParser::Warning(SMLoc L, const Twine &Msg) {
Sean Callananbf2013e2010-01-20 23:19:55 +000051 PrintMessage(L, Msg.str(), "warning");
Daniel Dunbar3fb76832009-06-30 00:49:23 +000052}
53
Daniel Dunbarf9507ff2009-07-27 23:20:52 +000054bool AsmParser::Error(SMLoc L, const Twine &Msg) {
Sean Callananbf2013e2010-01-20 23:19:55 +000055 PrintMessage(L, Msg.str(), "error");
Chris Lattner14ee48a2009-06-21 21:22:11 +000056 return true;
57}
58
59bool AsmParser::TokError(const char *Msg) {
Sean Callananbf2013e2010-01-20 23:19:55 +000060 PrintMessage(Lexer.getLoc(), Msg, "error");
Chris Lattner14ee48a2009-06-21 21:22:11 +000061 return true;
62}
63
Sean Callananbf2013e2010-01-20 23:19:55 +000064void AsmParser::PrintMessage(SMLoc Loc, const std::string &Msg,
65 const char *Type) const {
66 SrcMgr.PrintMessage(Loc, Msg, Type);
67}
Sean Callananfd0b0282010-01-21 00:19:58 +000068
69bool AsmParser::EnterIncludeFile(const std::string &Filename) {
70 int NewBuf = SrcMgr.AddIncludeFile(Filename, Lexer.getLoc());
71 if (NewBuf == -1)
72 return true;
Sean Callanan79036e42010-01-20 22:18:24 +000073
Sean Callananfd0b0282010-01-21 00:19:58 +000074 CurBuffer = NewBuf;
75
76 Lexer.setBuffer(SrcMgr.getMemoryBuffer(CurBuffer));
77
78 return false;
79}
80
81const AsmToken &AsmParser::Lex() {
82 const AsmToken *tok = &Lexer.Lex();
83
84 if (tok->is(AsmToken::Eof)) {
85 // If this is the end of an included file, pop the parent file off the
86 // include stack.
87 SMLoc ParentIncludeLoc = SrcMgr.getParentIncludeLoc(CurBuffer);
88 if (ParentIncludeLoc != SMLoc()) {
89 CurBuffer = SrcMgr.FindBufferContainingLoc(ParentIncludeLoc);
90 Lexer.setBuffer(SrcMgr.getMemoryBuffer(CurBuffer),
91 ParentIncludeLoc.getPointer());
92 tok = &Lexer.Lex();
93 }
94 }
95
96 if (tok->is(AsmToken::Error))
Sean Callananbf2013e2010-01-20 23:19:55 +000097 PrintMessage(Lexer.getErrLoc(), Lexer.getErr(), "error");
Sean Callanan79036e42010-01-20 22:18:24 +000098
Sean Callananfd0b0282010-01-21 00:19:58 +000099 return *tok;
Sean Callanan79ed1a82010-01-19 20:22:31 +0000100}
101
Chris Lattner79180e22010-04-05 23:15:42 +0000102bool AsmParser::Run(bool NoInitialTextSection, bool NoFinalize) {
Daniel Dunbar5e6a7a22010-03-13 02:20:57 +0000103 // Create the initial section, if requested.
Daniel Dunbar7c0a3342009-08-26 22:49:51 +0000104 //
Daniel Dunbar7c0a3342009-08-26 22:49:51 +0000105 // FIXME: Target hook & command line option for initial section.
Daniel Dunbar5e6a7a22010-03-13 02:20:57 +0000106 if (!NoInitialTextSection)
Chris Lattnerf0559e42010-04-08 20:30:37 +0000107 Out.SwitchSection(Ctx.getMachOSection("__TEXT", "__text",
Daniel Dunbar5e6a7a22010-03-13 02:20:57 +0000108 MCSectionMachO::S_ATTR_PURE_INSTRUCTIONS,
109 0, SectionKind::getText()));
Daniel Dunbar7c0a3342009-08-26 22:49:51 +0000110
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000111 // Prime the lexer.
Sean Callanan79ed1a82010-01-19 20:22:31 +0000112 Lex();
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000113
Chris Lattnerb717fb02009-07-02 21:53:43 +0000114 bool HadError = false;
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000115
Kevin Enderbyc114ed72009-08-07 22:46:00 +0000116 AsmCond StartingCondState = TheCondState;
117
Chris Lattnerb717fb02009-07-02 21:53:43 +0000118 // While we have input, parse each statement.
Daniel Dunbar3f872332009-07-28 16:08:33 +0000119 while (Lexer.isNot(AsmToken::Eof)) {
Kevin Enderbyc114ed72009-08-07 22:46:00 +0000120 // Handle conditional assembly here before calling ParseStatement()
121 if (Lexer.getKind() == AsmToken::Identifier) {
122 // If we have an identifier, handle it as the key symbol.
Sean Callanan18b83232010-01-19 21:44:56 +0000123 AsmToken ID = getTok();
Kevin Enderbyc114ed72009-08-07 22:46:00 +0000124 SMLoc IDLoc = ID.getLoc();
125 StringRef IDVal = ID.getString();
126
127 if (IDVal == ".if" ||
128 IDVal == ".elseif" ||
129 IDVal == ".else" ||
130 IDVal == ".endif") {
131 if (!ParseConditionalAssemblyDirectives(IDVal, IDLoc))
132 continue;
133 HadError = true;
134 EatToEndOfStatement();
135 continue;
136 }
137 }
138 if (TheCondState.Ignore) {
139 EatToEndOfStatement();
140 continue;
141 }
142
Chris Lattnerb717fb02009-07-02 21:53:43 +0000143 if (!ParseStatement()) continue;
144
Kevin Enderbyc114ed72009-08-07 22:46:00 +0000145 // We had an error, remember it and recover by skipping to the next line.
Chris Lattnerb717fb02009-07-02 21:53:43 +0000146 HadError = true;
147 EatToEndOfStatement();
148 }
Kevin Enderbyc114ed72009-08-07 22:46:00 +0000149
150 if (TheCondState.TheCond != StartingCondState.TheCond ||
151 TheCondState.Ignore != StartingCondState.Ignore)
152 return TokError("unmatched .ifs or .elses");
Chris Lattnerb717fb02009-07-02 21:53:43 +0000153
Chris Lattner79180e22010-04-05 23:15:42 +0000154 // Finalize the output stream if there are no errors and if the client wants
155 // us to.
156 if (!HadError && !NoFinalize)
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000157 Out.Finish();
158
Chris Lattnerb717fb02009-07-02 21:53:43 +0000159 return HadError;
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000160}
161
Kevin Enderbyc114ed72009-08-07 22:46:00 +0000162/// ParseConditionalAssemblyDirectives - parse the conditional assembly
163/// directives
164bool AsmParser::ParseConditionalAssemblyDirectives(StringRef Directive,
165 SMLoc DirectiveLoc) {
166 if (Directive == ".if")
167 return ParseDirectiveIf(DirectiveLoc);
168 if (Directive == ".elseif")
169 return ParseDirectiveElseIf(DirectiveLoc);
170 if (Directive == ".else")
171 return ParseDirectiveElse(DirectiveLoc);
172 if (Directive == ".endif")
173 return ParseDirectiveEndIf(DirectiveLoc);
174 return true;
175}
176
Chris Lattner2cf5f142009-06-22 01:29:09 +0000177/// EatToEndOfStatement - Throw away the rest of the line for testing purposes.
178void AsmParser::EatToEndOfStatement() {
Daniel Dunbar3f872332009-07-28 16:08:33 +0000179 while (Lexer.isNot(AsmToken::EndOfStatement) &&
180 Lexer.isNot(AsmToken::Eof))
Sean Callanan79ed1a82010-01-19 20:22:31 +0000181 Lex();
Chris Lattner2cf5f142009-06-22 01:29:09 +0000182
183 // Eat EOL.
Daniel Dunbar3f872332009-07-28 16:08:33 +0000184 if (Lexer.is(AsmToken::EndOfStatement))
Sean Callanan79ed1a82010-01-19 20:22:31 +0000185 Lex();
Chris Lattner2cf5f142009-06-22 01:29:09 +0000186}
187
Chris Lattnerc4193832009-06-22 05:51:26 +0000188
Chris Lattner74ec1a32009-06-22 06:32:03 +0000189/// ParseParenExpr - Parse a paren expression and return it.
190/// NOTE: This assumes the leading '(' has already been consumed.
191///
192/// parenexpr ::= expr)
193///
Chris Lattnerb4307b32010-01-15 19:28:38 +0000194bool AsmParser::ParseParenExpr(const MCExpr *&Res, SMLoc &EndLoc) {
Chris Lattner74ec1a32009-06-22 06:32:03 +0000195 if (ParseExpression(Res)) return true;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000196 if (Lexer.isNot(AsmToken::RParen))
Chris Lattner74ec1a32009-06-22 06:32:03 +0000197 return TokError("expected ')' in parentheses expression");
Chris Lattnerb4307b32010-01-15 19:28:38 +0000198 EndLoc = Lexer.getLoc();
Sean Callanan79ed1a82010-01-19 20:22:31 +0000199 Lex();
Chris Lattner74ec1a32009-06-22 06:32:03 +0000200 return false;
201}
Chris Lattnerc4193832009-06-22 05:51:26 +0000202
Daniel Dunbar959fd882009-08-26 22:13:22 +0000203MCSymbol *AsmParser::CreateSymbol(StringRef Name) {
Chris Lattner9b97a732010-03-30 18:10:53 +0000204 // FIXME: Inline into callers.
Chris Lattner00685bb2010-03-10 01:29:27 +0000205 return Ctx.GetOrCreateSymbol(Name);
Daniel Dunbar959fd882009-08-26 22:13:22 +0000206}
207
Chris Lattner74ec1a32009-06-22 06:32:03 +0000208/// ParsePrimaryExpr - Parse a primary expression and return it.
209/// primaryexpr ::= (parenexpr
210/// primaryexpr ::= symbol
211/// primaryexpr ::= number
212/// primaryexpr ::= ~,+,- primaryexpr
Chris Lattnerb4307b32010-01-15 19:28:38 +0000213bool AsmParser::ParsePrimaryExpr(const MCExpr *&Res, SMLoc &EndLoc) {
Chris Lattnerc4193832009-06-22 05:51:26 +0000214 switch (Lexer.getKind()) {
215 default:
216 return TokError("unknown token in expression");
Daniel Dunbar3f872332009-07-28 16:08:33 +0000217 case AsmToken::Exclaim:
Sean Callanan79ed1a82010-01-19 20:22:31 +0000218 Lex(); // Eat the operator.
Chris Lattnerb4307b32010-01-15 19:28:38 +0000219 if (ParsePrimaryExpr(Res, EndLoc))
Daniel Dunbar475839e2009-06-29 20:37:27 +0000220 return true;
Daniel Dunbar6ce004d2009-08-31 08:07:44 +0000221 Res = MCUnaryExpr::CreateLNot(Res, getContext());
Daniel Dunbar475839e2009-06-29 20:37:27 +0000222 return false;
Daniel Dunbar76c4d762009-07-31 21:55:09 +0000223 case AsmToken::String:
Daniel Dunbarfffff912009-10-16 01:34:54 +0000224 case AsmToken::Identifier: {
225 // This is a symbol reference.
Daniel Dunbar4e815f82010-03-15 23:51:06 +0000226 std::pair<StringRef, StringRef> Split = getTok().getIdentifier().split('@');
227 MCSymbol *Sym = CreateSymbol(Split.first);
228
229 // Lookup the symbol variant if used.
230 MCSymbolRefExpr::VariantKind Variant = MCSymbolRefExpr::VK_None;
231 if (Split.first.size() != getTok().getIdentifier().size())
232 Variant = MCSymbolRefExpr::getVariantKindForName(Split.second);
233
Chris Lattnerb4307b32010-01-15 19:28:38 +0000234 EndLoc = Lexer.getLoc();
Sean Callanan79ed1a82010-01-19 20:22:31 +0000235 Lex(); // Eat identifier.
Daniel Dunbarfffff912009-10-16 01:34:54 +0000236
237 // If this is an absolute variable reference, substitute it now to preserve
238 // semantics in the face of reassignment.
239 if (Sym->getValue() && isa<MCConstantExpr>(Sym->getValue())) {
Daniel Dunbar4e815f82010-03-15 23:51:06 +0000240 if (Variant)
241 return Error(EndLoc, "unexpected modified on variable reference");
242
Daniel Dunbarfffff912009-10-16 01:34:54 +0000243 Res = Sym->getValue();
244 return false;
245 }
246
247 // Otherwise create a symbol ref.
Daniel Dunbar4e815f82010-03-15 23:51:06 +0000248 Res = MCSymbolRefExpr::Create(Sym, Variant, getContext());
Chris Lattnerc4193832009-06-22 05:51:26 +0000249 return false;
Daniel Dunbarfffff912009-10-16 01:34:54 +0000250 }
Daniel Dunbar3f872332009-07-28 16:08:33 +0000251 case AsmToken::Integer:
Sean Callanan18b83232010-01-19 21:44:56 +0000252 Res = MCConstantExpr::Create(getTok().getIntVal(), getContext());
Chris Lattnerb4307b32010-01-15 19:28:38 +0000253 EndLoc = Lexer.getLoc();
Sean Callanan79ed1a82010-01-19 20:22:31 +0000254 Lex(); // Eat token.
Chris Lattnerc4193832009-06-22 05:51:26 +0000255 return false;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000256 case AsmToken::LParen:
Sean Callanan79ed1a82010-01-19 20:22:31 +0000257 Lex(); // Eat the '('.
Chris Lattnerb4307b32010-01-15 19:28:38 +0000258 return ParseParenExpr(Res, EndLoc);
Daniel Dunbar3f872332009-07-28 16:08:33 +0000259 case AsmToken::Minus:
Sean Callanan79ed1a82010-01-19 20:22:31 +0000260 Lex(); // Eat the operator.
Chris Lattnerb4307b32010-01-15 19:28:38 +0000261 if (ParsePrimaryExpr(Res, EndLoc))
Daniel Dunbar475839e2009-06-29 20:37:27 +0000262 return true;
Daniel Dunbar6ce004d2009-08-31 08:07:44 +0000263 Res = MCUnaryExpr::CreateMinus(Res, getContext());
Daniel Dunbar475839e2009-06-29 20:37:27 +0000264 return false;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000265 case AsmToken::Plus:
Sean Callanan79ed1a82010-01-19 20:22:31 +0000266 Lex(); // Eat the operator.
Chris Lattnerb4307b32010-01-15 19:28:38 +0000267 if (ParsePrimaryExpr(Res, EndLoc))
Daniel Dunbar475839e2009-06-29 20:37:27 +0000268 return true;
Daniel Dunbar6ce004d2009-08-31 08:07:44 +0000269 Res = MCUnaryExpr::CreatePlus(Res, getContext());
Daniel Dunbar475839e2009-06-29 20:37:27 +0000270 return false;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000271 case AsmToken::Tilde:
Sean Callanan79ed1a82010-01-19 20:22:31 +0000272 Lex(); // Eat the operator.
Chris Lattnerb4307b32010-01-15 19:28:38 +0000273 if (ParsePrimaryExpr(Res, EndLoc))
Daniel Dunbar475839e2009-06-29 20:37:27 +0000274 return true;
Daniel Dunbar6ce004d2009-08-31 08:07:44 +0000275 Res = MCUnaryExpr::CreateNot(Res, getContext());
Daniel Dunbar475839e2009-06-29 20:37:27 +0000276 return false;
Chris Lattnerc4193832009-06-22 05:51:26 +0000277 }
278}
Chris Lattner74ec1a32009-06-22 06:32:03 +0000279
Chris Lattnerb4307b32010-01-15 19:28:38 +0000280bool AsmParser::ParseExpression(const MCExpr *&Res) {
Chris Lattner54482b42010-01-15 19:39:23 +0000281 SMLoc EndLoc;
282 return ParseExpression(Res, EndLoc);
Chris Lattnerb4307b32010-01-15 19:28:38 +0000283}
284
Chris Lattner74ec1a32009-06-22 06:32:03 +0000285/// ParseExpression - Parse an expression and return it.
286///
287/// expr ::= expr +,- expr -> lowest.
288/// expr ::= expr |,^,&,! expr -> middle.
289/// expr ::= expr *,/,%,<<,>> expr -> highest.
290/// expr ::= primaryexpr
291///
Chris Lattner54482b42010-01-15 19:39:23 +0000292bool AsmParser::ParseExpression(const MCExpr *&Res, SMLoc &EndLoc) {
Daniel Dunbare9a60eb2010-02-13 01:28:07 +0000293 // Parse the expression.
Daniel Dunbar475839e2009-06-29 20:37:27 +0000294 Res = 0;
Daniel Dunbare9a60eb2010-02-13 01:28:07 +0000295 if (ParsePrimaryExpr(Res, EndLoc) || ParseBinOpRHS(1, Res, EndLoc))
296 return true;
297
298 // Try to constant fold it up front, if possible.
299 int64_t Value;
300 if (Res->EvaluateAsAbsolute(Value))
301 Res = MCConstantExpr::Create(Value, getContext());
302
303 return false;
Chris Lattner74ec1a32009-06-22 06:32:03 +0000304}
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000305
Chris Lattnerb4307b32010-01-15 19:28:38 +0000306bool AsmParser::ParseParenExpression(const MCExpr *&Res, SMLoc &EndLoc) {
Chris Lattner75f265f2010-01-24 01:07:33 +0000307 Res = 0;
308 return ParseParenExpr(Res, EndLoc) ||
309 ParseBinOpRHS(1, Res, EndLoc);
Daniel Dunbarc18274b2009-08-31 08:08:17 +0000310}
311
Daniel Dunbar475839e2009-06-29 20:37:27 +0000312bool AsmParser::ParseAbsoluteExpression(int64_t &Res) {
Daniel Dunbar9643ac52009-08-31 08:07:22 +0000313 const MCExpr *Expr;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000314
Daniel Dunbarf4b830f2009-06-30 02:10:03 +0000315 SMLoc StartLoc = Lexer.getLoc();
Daniel Dunbar475839e2009-06-29 20:37:27 +0000316 if (ParseExpression(Expr))
317 return true;
318
Daniel Dunbare00b0112009-10-16 01:57:52 +0000319 if (!Expr->EvaluateAsAbsolute(Res))
Daniel Dunbarf4b830f2009-06-30 02:10:03 +0000320 return Error(StartLoc, "expected absolute expression");
Daniel Dunbar475839e2009-06-29 20:37:27 +0000321
322 return false;
323}
324
Daniel Dunbar3f872332009-07-28 16:08:33 +0000325static unsigned getBinOpPrecedence(AsmToken::TokenKind K,
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000326 MCBinaryExpr::Opcode &Kind) {
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000327 switch (K) {
Daniel Dunbar6ce004d2009-08-31 08:07:44 +0000328 default:
329 return 0; // not a binop.
Daniel Dunbar475839e2009-06-29 20:37:27 +0000330
331 // Lowest Precedence: &&, ||
Daniel Dunbar3f872332009-07-28 16:08:33 +0000332 case AsmToken::AmpAmp:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000333 Kind = MCBinaryExpr::LAnd;
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000334 return 1;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000335 case AsmToken::PipePipe:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000336 Kind = MCBinaryExpr::LOr;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000337 return 1;
338
339 // Low Precedence: +, -, ==, !=, <>, <, <=, >, >=
Daniel Dunbar3f872332009-07-28 16:08:33 +0000340 case AsmToken::Plus:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000341 Kind = MCBinaryExpr::Add;
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000342 return 2;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000343 case AsmToken::Minus:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000344 Kind = MCBinaryExpr::Sub;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000345 return 2;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000346 case AsmToken::EqualEqual:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000347 Kind = MCBinaryExpr::EQ;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000348 return 2;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000349 case AsmToken::ExclaimEqual:
350 case AsmToken::LessGreater:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000351 Kind = MCBinaryExpr::NE;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000352 return 2;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000353 case AsmToken::Less:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000354 Kind = MCBinaryExpr::LT;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000355 return 2;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000356 case AsmToken::LessEqual:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000357 Kind = MCBinaryExpr::LTE;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000358 return 2;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000359 case AsmToken::Greater:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000360 Kind = MCBinaryExpr::GT;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000361 return 2;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000362 case AsmToken::GreaterEqual:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000363 Kind = MCBinaryExpr::GTE;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000364 return 2;
365
366 // Intermediate Precedence: |, &, ^
367 //
368 // FIXME: gas seems to support '!' as an infix operator?
Daniel Dunbar3f872332009-07-28 16:08:33 +0000369 case AsmToken::Pipe:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000370 Kind = MCBinaryExpr::Or;
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000371 return 3;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000372 case AsmToken::Caret:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000373 Kind = MCBinaryExpr::Xor;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000374 return 3;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000375 case AsmToken::Amp:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000376 Kind = MCBinaryExpr::And;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000377 return 3;
378
379 // Highest Precedence: *, /, %, <<, >>
Daniel Dunbar3f872332009-07-28 16:08:33 +0000380 case AsmToken::Star:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000381 Kind = MCBinaryExpr::Mul;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000382 return 4;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000383 case AsmToken::Slash:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000384 Kind = MCBinaryExpr::Div;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000385 return 4;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000386 case AsmToken::Percent:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000387 Kind = MCBinaryExpr::Mod;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000388 return 4;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000389 case AsmToken::LessLess:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000390 Kind = MCBinaryExpr::Shl;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000391 return 4;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000392 case AsmToken::GreaterGreater:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000393 Kind = MCBinaryExpr::Shr;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000394 return 4;
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000395 }
396}
397
398
399/// ParseBinOpRHS - Parse all binary operators with precedence >= 'Precedence'.
400/// Res contains the LHS of the expression on input.
Chris Lattnerb4307b32010-01-15 19:28:38 +0000401bool AsmParser::ParseBinOpRHS(unsigned Precedence, const MCExpr *&Res,
402 SMLoc &EndLoc) {
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000403 while (1) {
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000404 MCBinaryExpr::Opcode Kind = MCBinaryExpr::Add;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000405 unsigned TokPrec = getBinOpPrecedence(Lexer.getKind(), Kind);
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000406
407 // If the next token is lower precedence than we are allowed to eat, return
408 // successfully with what we ate already.
409 if (TokPrec < Precedence)
410 return false;
411
Sean Callanan79ed1a82010-01-19 20:22:31 +0000412 Lex();
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000413
414 // Eat the next primary expression.
Daniel Dunbar9643ac52009-08-31 08:07:22 +0000415 const MCExpr *RHS;
Chris Lattnerb4307b32010-01-15 19:28:38 +0000416 if (ParsePrimaryExpr(RHS, EndLoc)) return true;
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000417
418 // If BinOp binds less tightly with RHS than the operator after RHS, let
419 // the pending operator take RHS as its LHS.
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000420 MCBinaryExpr::Opcode Dummy;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000421 unsigned NextTokPrec = getBinOpPrecedence(Lexer.getKind(), Dummy);
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000422 if (TokPrec < NextTokPrec) {
Chris Lattnerb4307b32010-01-15 19:28:38 +0000423 if (ParseBinOpRHS(Precedence+1, RHS, EndLoc)) return true;
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000424 }
425
Daniel Dunbar475839e2009-06-29 20:37:27 +0000426 // Merge LHS and RHS according to operator.
Daniel Dunbar6ce004d2009-08-31 08:07:44 +0000427 Res = MCBinaryExpr::Create(Kind, Res, RHS, getContext());
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000428 }
429}
430
Chris Lattnerc4193832009-06-22 05:51:26 +0000431
432
433
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000434/// ParseStatement:
435/// ::= EndOfStatement
Chris Lattner2cf5f142009-06-22 01:29:09 +0000436/// ::= Label* Directive ...Operands... EndOfStatement
437/// ::= Label* Identifier OperandList* EndOfStatement
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000438bool AsmParser::ParseStatement() {
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +0000439 if (Lexer.is(AsmToken::EndOfStatement)) {
Sean Callanan79ed1a82010-01-19 20:22:31 +0000440 Lex();
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000441 return false;
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000442 }
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +0000443
444 // Statements always start with an identifier.
Sean Callanan18b83232010-01-19 21:44:56 +0000445 AsmToken ID = getTok();
Daniel Dunbar419aded2009-07-28 16:38:40 +0000446 SMLoc IDLoc = ID.getLoc();
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +0000447 StringRef IDVal;
448 if (ParseIdentifier(IDVal))
449 return TokError("unexpected token at start of statement");
450
451 // FIXME: Recurse on local labels?
452
453 // See what kind of statement we have.
454 switch (Lexer.getKind()) {
Daniel Dunbar3f872332009-07-28 16:08:33 +0000455 case AsmToken::Colon: {
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000456 // identifier ':' -> Label.
Sean Callanan79ed1a82010-01-19 20:22:31 +0000457 Lex();
Daniel Dunbardce0f3c2009-06-29 23:43:14 +0000458
459 // Diagnose attempt to use a variable as a label.
460 //
461 // FIXME: Diagnostics. Note the location of the definition as a label.
462 // FIXME: This doesn't diagnose assignment to a symbol which has been
463 // implicitly marked as external.
Daniel Dunbar959fd882009-08-26 22:13:22 +0000464 MCSymbol *Sym = CreateSymbol(IDVal);
Daniel Dunbar8906ff12009-08-22 07:22:36 +0000465 if (!Sym->isUndefined())
Daniel Dunbardce0f3c2009-06-29 23:43:14 +0000466 return Error(IDLoc, "invalid symbol redefinition");
Chris Lattnerc69485e2009-06-24 04:31:49 +0000467
Daniel Dunbar959fd882009-08-26 22:13:22 +0000468 // Emit the label.
Daniel Dunbardce0f3c2009-06-29 23:43:14 +0000469 Out.EmitLabel(Sym);
470
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000471 return ParseStatement();
Daniel Dunbardce0f3c2009-06-29 23:43:14 +0000472 }
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000473
Daniel Dunbar3f872332009-07-28 16:08:33 +0000474 case AsmToken::Equal:
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000475 // identifier '=' ... -> assignment statement
Sean Callanan79ed1a82010-01-19 20:22:31 +0000476 Lex();
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000477
Daniel Dunbare2ace502009-08-31 08:09:09 +0000478 return ParseAssignment(IDVal);
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000479
480 default: // Normal instruction or directive.
481 break;
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000482 }
483
484 // Otherwise, we have a normal instruction or directive.
Chris Lattner2cf5f142009-06-22 01:29:09 +0000485 if (IDVal[0] == '.') {
Chris Lattner529fb542009-06-24 05:13:15 +0000486 // FIXME: This should be driven based on a hash lookup and callback.
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000487 if (IDVal == ".section")
Chris Lattner529fb542009-06-24 05:13:15 +0000488 return ParseDirectiveDarwinSection();
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000489 if (IDVal == ".text")
Chris Lattner529fb542009-06-24 05:13:15 +0000490 // FIXME: This changes behavior based on the -static flag to the
491 // assembler.
Chris Lattnerff4bc462009-08-10 01:39:42 +0000492 return ParseDirectiveSectionSwitch("__TEXT", "__text",
493 MCSectionMachO::S_ATTR_PURE_INSTRUCTIONS);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000494 if (IDVal == ".const")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000495 return ParseDirectiveSectionSwitch("__TEXT", "__const");
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000496 if (IDVal == ".static_const")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000497 return ParseDirectiveSectionSwitch("__TEXT", "__static_const");
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000498 if (IDVal == ".cstring")
Chris Lattnerff4bc462009-08-10 01:39:42 +0000499 return ParseDirectiveSectionSwitch("__TEXT","__cstring",
500 MCSectionMachO::S_CSTRING_LITERALS);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000501 if (IDVal == ".literal4")
Chris Lattnerff4bc462009-08-10 01:39:42 +0000502 return ParseDirectiveSectionSwitch("__TEXT", "__literal4",
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000503 MCSectionMachO::S_4BYTE_LITERALS,
504 4);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000505 if (IDVal == ".literal8")
Chris Lattnerff4bc462009-08-10 01:39:42 +0000506 return ParseDirectiveSectionSwitch("__TEXT", "__literal8",
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000507 MCSectionMachO::S_8BYTE_LITERALS,
508 8);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000509 if (IDVal == ".literal16")
Chris Lattnerff4bc462009-08-10 01:39:42 +0000510 return ParseDirectiveSectionSwitch("__TEXT","__literal16",
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000511 MCSectionMachO::S_16BYTE_LITERALS,
512 16);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000513 if (IDVal == ".constructor")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000514 return ParseDirectiveSectionSwitch("__TEXT","__constructor");
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000515 if (IDVal == ".destructor")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000516 return ParseDirectiveSectionSwitch("__TEXT","__destructor");
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000517 if (IDVal == ".fvmlib_init0")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000518 return ParseDirectiveSectionSwitch("__TEXT","__fvmlib_init0");
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000519 if (IDVal == ".fvmlib_init1")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000520 return ParseDirectiveSectionSwitch("__TEXT","__fvmlib_init1");
521
522 // FIXME: The assembler manual claims that this has the self modify code
523 // flag, at least on x86-32, but that does not appear to be correct.
Chris Lattnerff4bc462009-08-10 01:39:42 +0000524 if (IDVal == ".symbol_stub")
525 return ParseDirectiveSectionSwitch("__TEXT","__symbol_stub",
526 MCSectionMachO::S_SYMBOL_STUBS |
Chris Lattnerff4bc462009-08-10 01:39:42 +0000527 MCSectionMachO::S_ATTR_PURE_INSTRUCTIONS,
528 // FIXME: Different on PPC and ARM.
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000529 0, 16);
530 // FIXME: PowerPC only?
531 if (IDVal == ".picsymbol_stub")
532 return ParseDirectiveSectionSwitch("__TEXT","__picsymbol_stub",
533 MCSectionMachO::S_SYMBOL_STUBS |
534 MCSectionMachO::S_ATTR_PURE_INSTRUCTIONS,
535 0, 26);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000536 if (IDVal == ".data")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000537 return ParseDirectiveSectionSwitch("__DATA", "__data");
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000538 if (IDVal == ".static_data")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000539 return ParseDirectiveSectionSwitch("__DATA", "__static_data");
540
541 // FIXME: The section names of these two are misspelled in the assembler
542 // manual.
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000543 if (IDVal == ".non_lazy_symbol_pointer")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000544 return ParseDirectiveSectionSwitch("__DATA", "__nl_symbol_ptr",
545 MCSectionMachO::S_NON_LAZY_SYMBOL_POINTERS,
546 4);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000547 if (IDVal == ".lazy_symbol_pointer")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000548 return ParseDirectiveSectionSwitch("__DATA", "__la_symbol_ptr",
549 MCSectionMachO::S_LAZY_SYMBOL_POINTERS,
550 4);
551
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000552 if (IDVal == ".dyld")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000553 return ParseDirectiveSectionSwitch("__DATA", "__dyld");
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000554 if (IDVal == ".mod_init_func")
Chris Lattnerff4bc462009-08-10 01:39:42 +0000555 return ParseDirectiveSectionSwitch("__DATA", "__mod_init_func",
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000556 MCSectionMachO::S_MOD_INIT_FUNC_POINTERS,
557 4);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000558 if (IDVal == ".mod_term_func")
Chris Lattnerff4bc462009-08-10 01:39:42 +0000559 return ParseDirectiveSectionSwitch("__DATA", "__mod_term_func",
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000560 MCSectionMachO::S_MOD_TERM_FUNC_POINTERS,
561 4);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000562 if (IDVal == ".const_data")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000563 return ParseDirectiveSectionSwitch("__DATA", "__const");
Chris Lattner529fb542009-06-24 05:13:15 +0000564
565
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000566 if (IDVal == ".objc_class")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000567 return ParseDirectiveSectionSwitch("__OBJC", "__class",
568 MCSectionMachO::S_ATTR_NO_DEAD_STRIP);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000569 if (IDVal == ".objc_meta_class")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000570 return ParseDirectiveSectionSwitch("__OBJC", "__meta_class",
571 MCSectionMachO::S_ATTR_NO_DEAD_STRIP);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000572 if (IDVal == ".objc_cat_cls_meth")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000573 return ParseDirectiveSectionSwitch("__OBJC", "__cat_cls_meth",
574 MCSectionMachO::S_ATTR_NO_DEAD_STRIP);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000575 if (IDVal == ".objc_cat_inst_meth")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000576 return ParseDirectiveSectionSwitch("__OBJC", "__cat_inst_meth",
577 MCSectionMachO::S_ATTR_NO_DEAD_STRIP);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000578 if (IDVal == ".objc_protocol")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000579 return ParseDirectiveSectionSwitch("__OBJC", "__protocol",
580 MCSectionMachO::S_ATTR_NO_DEAD_STRIP);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000581 if (IDVal == ".objc_string_object")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000582 return ParseDirectiveSectionSwitch("__OBJC", "__string_object",
583 MCSectionMachO::S_ATTR_NO_DEAD_STRIP);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000584 if (IDVal == ".objc_cls_meth")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000585 return ParseDirectiveSectionSwitch("__OBJC", "__cls_meth",
586 MCSectionMachO::S_ATTR_NO_DEAD_STRIP);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000587 if (IDVal == ".objc_inst_meth")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000588 return ParseDirectiveSectionSwitch("__OBJC", "__inst_meth",
589 MCSectionMachO::S_ATTR_NO_DEAD_STRIP);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000590 if (IDVal == ".objc_cls_refs")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000591 return ParseDirectiveSectionSwitch("__OBJC", "__cls_refs",
592 MCSectionMachO::S_ATTR_NO_DEAD_STRIP |
593 MCSectionMachO::S_LITERAL_POINTERS,
594 4);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000595 if (IDVal == ".objc_message_refs")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000596 return ParseDirectiveSectionSwitch("__OBJC", "__message_refs",
597 MCSectionMachO::S_ATTR_NO_DEAD_STRIP |
598 MCSectionMachO::S_LITERAL_POINTERS,
599 4);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000600 if (IDVal == ".objc_symbols")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000601 return ParseDirectiveSectionSwitch("__OBJC", "__symbols",
602 MCSectionMachO::S_ATTR_NO_DEAD_STRIP);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000603 if (IDVal == ".objc_category")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000604 return ParseDirectiveSectionSwitch("__OBJC", "__category",
605 MCSectionMachO::S_ATTR_NO_DEAD_STRIP);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000606 if (IDVal == ".objc_class_vars")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000607 return ParseDirectiveSectionSwitch("__OBJC", "__class_vars",
608 MCSectionMachO::S_ATTR_NO_DEAD_STRIP);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000609 if (IDVal == ".objc_instance_vars")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000610 return ParseDirectiveSectionSwitch("__OBJC", "__instance_vars",
611 MCSectionMachO::S_ATTR_NO_DEAD_STRIP);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000612 if (IDVal == ".objc_module_info")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000613 return ParseDirectiveSectionSwitch("__OBJC", "__module_info",
614 MCSectionMachO::S_ATTR_NO_DEAD_STRIP);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000615 if (IDVal == ".objc_class_names")
Chris Lattnerff4bc462009-08-10 01:39:42 +0000616 return ParseDirectiveSectionSwitch("__TEXT", "__cstring",
617 MCSectionMachO::S_CSTRING_LITERALS);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000618 if (IDVal == ".objc_meth_var_types")
Chris Lattnerff4bc462009-08-10 01:39:42 +0000619 return ParseDirectiveSectionSwitch("__TEXT", "__cstring",
620 MCSectionMachO::S_CSTRING_LITERALS);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000621 if (IDVal == ".objc_meth_var_names")
Chris Lattnerff4bc462009-08-10 01:39:42 +0000622 return ParseDirectiveSectionSwitch("__TEXT", "__cstring",
623 MCSectionMachO::S_CSTRING_LITERALS);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000624 if (IDVal == ".objc_selector_strs")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000625 return ParseDirectiveSectionSwitch("__OBJC", "__selector_strs",
626 MCSectionMachO::S_CSTRING_LITERALS);
Chris Lattner9a023f72009-06-24 04:43:34 +0000627
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000628 // Assembler features
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000629 if (IDVal == ".set")
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000630 return ParseDirectiveSet();
631
Daniel Dunbara0d14262009-06-24 23:30:00 +0000632 // Data directives
633
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000634 if (IDVal == ".ascii")
Daniel Dunbara0d14262009-06-24 23:30:00 +0000635 return ParseDirectiveAscii(false);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000636 if (IDVal == ".asciz")
Daniel Dunbara0d14262009-06-24 23:30:00 +0000637 return ParseDirectiveAscii(true);
638
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000639 if (IDVal == ".byte")
Daniel Dunbara0d14262009-06-24 23:30:00 +0000640 return ParseDirectiveValue(1);
Kevin Enderby9c656452009-09-10 20:51:44 +0000641 if (IDVal == ".short")
Daniel Dunbara0d14262009-06-24 23:30:00 +0000642 return ParseDirectiveValue(2);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000643 if (IDVal == ".long")
Daniel Dunbara0d14262009-06-24 23:30:00 +0000644 return ParseDirectiveValue(4);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000645 if (IDVal == ".quad")
Daniel Dunbara0d14262009-06-24 23:30:00 +0000646 return ParseDirectiveValue(8);
Daniel Dunbarc29dfa72009-06-29 23:46:59 +0000647
648 // FIXME: Target hooks for IsPow2.
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000649 if (IDVal == ".align")
Daniel Dunbarc29dfa72009-06-29 23:46:59 +0000650 return ParseDirectiveAlign(/*IsPow2=*/true, /*ExprSize=*/1);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000651 if (IDVal == ".align32")
Daniel Dunbarc29dfa72009-06-29 23:46:59 +0000652 return ParseDirectiveAlign(/*IsPow2=*/true, /*ExprSize=*/4);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000653 if (IDVal == ".balign")
Daniel Dunbarc29dfa72009-06-29 23:46:59 +0000654 return ParseDirectiveAlign(/*IsPow2=*/false, /*ExprSize=*/1);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000655 if (IDVal == ".balignw")
Daniel Dunbarc29dfa72009-06-29 23:46:59 +0000656 return ParseDirectiveAlign(/*IsPow2=*/false, /*ExprSize=*/2);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000657 if (IDVal == ".balignl")
Daniel Dunbarc29dfa72009-06-29 23:46:59 +0000658 return ParseDirectiveAlign(/*IsPow2=*/false, /*ExprSize=*/4);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000659 if (IDVal == ".p2align")
Daniel Dunbarc29dfa72009-06-29 23:46:59 +0000660 return ParseDirectiveAlign(/*IsPow2=*/true, /*ExprSize=*/1);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000661 if (IDVal == ".p2alignw")
Daniel Dunbarc29dfa72009-06-29 23:46:59 +0000662 return ParseDirectiveAlign(/*IsPow2=*/true, /*ExprSize=*/2);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000663 if (IDVal == ".p2alignl")
Daniel Dunbarc29dfa72009-06-29 23:46:59 +0000664 return ParseDirectiveAlign(/*IsPow2=*/true, /*ExprSize=*/4);
665
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000666 if (IDVal == ".org")
Daniel Dunbarc238b582009-06-25 22:44:51 +0000667 return ParseDirectiveOrg();
Daniel Dunbarc29dfa72009-06-29 23:46:59 +0000668
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000669 if (IDVal == ".fill")
Daniel Dunbarc29dfa72009-06-29 23:46:59 +0000670 return ParseDirectiveFill();
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000671 if (IDVal == ".space")
Daniel Dunbara0d14262009-06-24 23:30:00 +0000672 return ParseDirectiveSpace();
673
Daniel Dunbard7b267b2009-06-30 00:33:19 +0000674 // Symbol attribute directives
Daniel Dunbard0c14d62009-08-11 04:24:50 +0000675
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000676 if (IDVal == ".globl" || IDVal == ".global")
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000677 return ParseDirectiveSymbolAttribute(MCSA_Global);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000678 if (IDVal == ".hidden")
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000679 return ParseDirectiveSymbolAttribute(MCSA_Hidden);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000680 if (IDVal == ".indirect_symbol")
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000681 return ParseDirectiveSymbolAttribute(MCSA_IndirectSymbol);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000682 if (IDVal == ".internal")
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000683 return ParseDirectiveSymbolAttribute(MCSA_Internal);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000684 if (IDVal == ".lazy_reference")
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000685 return ParseDirectiveSymbolAttribute(MCSA_LazyReference);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000686 if (IDVal == ".no_dead_strip")
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000687 return ParseDirectiveSymbolAttribute(MCSA_NoDeadStrip);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000688 if (IDVal == ".private_extern")
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000689 return ParseDirectiveSymbolAttribute(MCSA_PrivateExtern);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000690 if (IDVal == ".protected")
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000691 return ParseDirectiveSymbolAttribute(MCSA_Protected);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000692 if (IDVal == ".reference")
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000693 return ParseDirectiveSymbolAttribute(MCSA_Reference);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000694 if (IDVal == ".weak")
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000695 return ParseDirectiveSymbolAttribute(MCSA_Weak);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000696 if (IDVal == ".weak_definition")
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000697 return ParseDirectiveSymbolAttribute(MCSA_WeakDefinition);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000698 if (IDVal == ".weak_reference")
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000699 return ParseDirectiveSymbolAttribute(MCSA_WeakReference);
Daniel Dunbard7b267b2009-06-30 00:33:19 +0000700
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000701 if (IDVal == ".comm")
Chris Lattner1fc3d752009-07-09 17:25:12 +0000702 return ParseDirectiveComm(/*IsLocal=*/false);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000703 if (IDVal == ".lcomm")
Chris Lattner1fc3d752009-07-09 17:25:12 +0000704 return ParseDirectiveComm(/*IsLocal=*/true);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000705 if (IDVal == ".zerofill")
Chris Lattner9be3fee2009-07-10 22:20:30 +0000706 return ParseDirectiveDarwinZerofill();
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000707 if (IDVal == ".desc")
Kevin Enderby95cf30c2009-07-14 18:17:10 +0000708 return ParseDirectiveDarwinSymbolDesc();
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000709 if (IDVal == ".lsym")
Kevin Enderby71148242009-07-14 21:35:03 +0000710 return ParseDirectiveDarwinLsym();
Chris Lattner4e4db7a2009-07-07 20:30:46 +0000711
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000712 if (IDVal == ".subsections_via_symbols")
Kevin Enderbya5c78322009-07-13 21:03:15 +0000713 return ParseDirectiveDarwinSubsectionsViaSymbols();
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000714 if (IDVal == ".abort")
Kevin Enderby5f1f0b82009-07-13 23:15:14 +0000715 return ParseDirectiveAbort();
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000716 if (IDVal == ".include")
Kevin Enderby1f049b22009-07-14 23:21:55 +0000717 return ParseDirectiveInclude();
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000718 if (IDVal == ".dump")
Kevin Enderby5026ae42009-07-20 20:25:37 +0000719 return ParseDirectiveDarwinDumpOrLoad(IDLoc, /*IsDump=*/true);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000720 if (IDVal == ".load")
Kevin Enderby5026ae42009-07-20 20:25:37 +0000721 return ParseDirectiveDarwinDumpOrLoad(IDLoc, /*IsLoad=*/false);
Kevin Enderbya5c78322009-07-13 21:03:15 +0000722
Chris Lattnerebb89b42009-09-27 21:16:52 +0000723 // Look up the handler in the handler table,
724 bool(AsmParser::*Handler)(StringRef, SMLoc) = DirectiveMap[IDVal];
725 if (Handler)
726 return (this->*Handler)(IDVal, IDLoc);
727
Kevin Enderby9c656452009-09-10 20:51:44 +0000728 // Target hook for parsing target specific directives.
729 if (!getTargetParser().ParseDirective(ID))
730 return false;
731
Daniel Dunbar3fb76832009-06-30 00:49:23 +0000732 Warning(IDLoc, "ignoring directive for now");
Chris Lattner2cf5f142009-06-22 01:29:09 +0000733 EatToEndOfStatement();
734 return false;
735 }
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000736
Chris Lattner98986712010-01-14 22:21:20 +0000737
738 SmallVector<MCParsedAsmOperand*, 8> ParsedOperands;
739 if (getTargetParser().ParseInstruction(IDVal, IDLoc, ParsedOperands))
740 // FIXME: Leaking ParsedOperands on failure.
Chris Lattner29dfe7c2009-06-23 18:41:30 +0000741 return true;
Chris Lattner2cf5f142009-06-22 01:29:09 +0000742
Daniel Dunbar3f872332009-07-28 16:08:33 +0000743 if (Lexer.isNot(AsmToken::EndOfStatement))
Chris Lattner98986712010-01-14 22:21:20 +0000744 // FIXME: Leaking ParsedOperands on failure.
Chris Lattner9a023f72009-06-24 04:43:34 +0000745 return TokError("unexpected token in argument list");
Chris Lattner2cf5f142009-06-22 01:29:09 +0000746
747 // Eat the end of statement marker.
Sean Callanan79ed1a82010-01-19 20:22:31 +0000748 Lex();
Chris Lattner2cf5f142009-06-22 01:29:09 +0000749
Chris Lattner98986712010-01-14 22:21:20 +0000750
751 MCInst Inst;
752
753 bool MatchFail = getTargetParser().MatchInstruction(ParsedOperands, Inst);
754
755 // Free any parsed operands.
756 for (unsigned i = 0, e = ParsedOperands.size(); i != e; ++i)
757 delete ParsedOperands[i];
758
759 if (MatchFail) {
760 // FIXME: We should give nicer diagnostics about the exact failure.
761 Error(IDLoc, "unrecognized instruction");
762 return true;
763 }
764
Chris Lattner2cf5f142009-06-22 01:29:09 +0000765 // Instruction is good, process it.
Daniel Dunbar0eebb052009-07-01 06:35:48 +0000766 Out.EmitInstruction(Inst);
Chris Lattner2cf5f142009-06-22 01:29:09 +0000767
768 // Skip to end of line for now.
Chris Lattner27aa7d22009-06-21 20:16:42 +0000769 return false;
770}
Chris Lattner9a023f72009-06-24 04:43:34 +0000771
Daniel Dunbare2ace502009-08-31 08:09:09 +0000772bool AsmParser::ParseAssignment(const StringRef &Name) {
Daniel Dunbardce0f3c2009-06-29 23:43:14 +0000773 // FIXME: Use better location, we should use proper tokens.
774 SMLoc EqualLoc = Lexer.getLoc();
775
Daniel Dunbar821e3332009-08-31 08:09:28 +0000776 const MCExpr *Value;
Daniel Dunbar883f9202009-08-31 08:08:50 +0000777 SMLoc StartLoc = Lexer.getLoc();
Daniel Dunbar821e3332009-08-31 08:09:28 +0000778 if (ParseExpression(Value))
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000779 return true;
780
Daniel Dunbar3f872332009-07-28 16:08:33 +0000781 if (Lexer.isNot(AsmToken::EndOfStatement))
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000782 return TokError("unexpected token in assignment");
783
784 // Eat the end of statement marker.
Sean Callanan79ed1a82010-01-19 20:22:31 +0000785 Lex();
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000786
Daniel Dunbar75773ff2009-10-16 01:57:39 +0000787 // Validate that the LHS is allowed to be a variable (either it has not been
788 // used as a symbol, or it is an absolute symbol).
789 MCSymbol *Sym = getContext().LookupSymbol(Name);
790 if (Sym) {
791 // Diagnose assignment to a label.
792 //
793 // FIXME: Diagnostics. Note the location of the definition as a label.
794 // FIXME: Diagnose assignment to protected identifier (e.g., register name).
795 if (!Sym->isUndefined() && !Sym->isAbsolute())
796 return Error(EqualLoc, "redefinition of '" + Name + "'");
797 else if (!Sym->isVariable())
798 return Error(EqualLoc, "invalid assignment to '" + Name + "'");
799 else if (!isa<MCConstantExpr>(Sym->getValue()))
800 return Error(EqualLoc, "invalid reassignment of non-absolute variable '" +
801 Name + "'");
802 } else
803 Sym = CreateSymbol(Name);
804
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000805 // FIXME: Handle '.'.
Daniel Dunbardce0f3c2009-06-29 23:43:14 +0000806
807 // Do the assignment.
Daniel Dunbare2ace502009-08-31 08:09:09 +0000808 Out.EmitAssignment(Sym, Value);
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000809
810 return false;
811}
812
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +0000813/// ParseIdentifier:
814/// ::= identifier
815/// ::= string
816bool AsmParser::ParseIdentifier(StringRef &Res) {
817 if (Lexer.isNot(AsmToken::Identifier) &&
818 Lexer.isNot(AsmToken::String))
819 return true;
820
Sean Callanan18b83232010-01-19 21:44:56 +0000821 Res = getTok().getIdentifier();
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +0000822
Sean Callanan79ed1a82010-01-19 20:22:31 +0000823 Lex(); // Consume the identifier token.
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +0000824
825 return false;
826}
827
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000828/// ParseDirectiveSet:
829/// ::= .set identifier ',' expression
830bool AsmParser::ParseDirectiveSet() {
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +0000831 StringRef Name;
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000832
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +0000833 if (ParseIdentifier(Name))
834 return TokError("expected identifier after '.set' directive");
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000835
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +0000836 if (Lexer.isNot(AsmToken::Comma))
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000837 return TokError("unexpected token in '.set'");
Sean Callanan79ed1a82010-01-19 20:22:31 +0000838 Lex();
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000839
Daniel Dunbare2ace502009-08-31 08:09:09 +0000840 return ParseAssignment(Name);
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000841}
842
Chris Lattner9a023f72009-06-24 04:43:34 +0000843/// ParseDirectiveSection:
Chris Lattner529fb542009-06-24 05:13:15 +0000844/// ::= .section identifier (',' identifier)*
845/// FIXME: This should actually parse out the segment, section, attributes and
846/// sizeof_stub fields.
847bool AsmParser::ParseDirectiveDarwinSection() {
Daniel Dunbarace63122009-08-11 03:42:33 +0000848 SMLoc Loc = Lexer.getLoc();
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +0000849
Daniel Dunbarace63122009-08-11 03:42:33 +0000850 StringRef SectionName;
851 if (ParseIdentifier(SectionName))
852 return Error(Loc, "expected identifier after '.section' directive");
853
854 // Verify there is a following comma.
855 if (!Lexer.is(AsmToken::Comma))
856 return TokError("unexpected token in '.section' directive");
857
Chris Lattnerff4bc462009-08-10 01:39:42 +0000858 std::string SectionSpec = SectionName;
Daniel Dunbarace63122009-08-11 03:42:33 +0000859 SectionSpec += ",";
860
861 // Add all the tokens until the end of the line, ParseSectionSpecifier will
862 // handle this.
Chris Lattnerff4bc462009-08-10 01:39:42 +0000863 StringRef EOL = Lexer.LexUntilEndOfStatement();
864 SectionSpec.append(EOL.begin(), EOL.end());
Daniel Dunbarace63122009-08-11 03:42:33 +0000865
Sean Callanan79ed1a82010-01-19 20:22:31 +0000866 Lex();
Daniel Dunbar3f872332009-07-28 16:08:33 +0000867 if (Lexer.isNot(AsmToken::EndOfStatement))
Chris Lattner9a023f72009-06-24 04:43:34 +0000868 return TokError("unexpected token in '.section' directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +0000869 Lex();
Chris Lattner9a023f72009-06-24 04:43:34 +0000870
Chris Lattnerff4bc462009-08-10 01:39:42 +0000871
872 StringRef Segment, Section;
873 unsigned TAA, StubSize;
874 std::string ErrorStr =
875 MCSectionMachO::ParseSectionSpecifier(SectionSpec, Segment, Section,
876 TAA, StubSize);
877
878 if (!ErrorStr.empty())
Daniel Dunbarace63122009-08-11 03:42:33 +0000879 return Error(Loc, ErrorStr.c_str());
Chris Lattnerff4bc462009-08-10 01:39:42 +0000880
Chris Lattner56594f92009-07-31 17:47:16 +0000881 // FIXME: Arch specific.
Chris Lattnerf60e9bb2010-02-26 18:32:26 +0000882 bool isText = Segment == "__TEXT"; // FIXME: Hack.
Chris Lattnerf0559e42010-04-08 20:30:37 +0000883 Out.SwitchSection(Ctx.getMachOSection(Segment, Section, TAA, StubSize,
884 isText ? SectionKind::getText()
885 : SectionKind::getDataRel()));
Chris Lattner9a023f72009-06-24 04:43:34 +0000886 return false;
887}
888
Chris Lattnere15c2d72009-08-10 18:05:55 +0000889/// ParseDirectiveSectionSwitch -
Chris Lattnerff4bc462009-08-10 01:39:42 +0000890bool AsmParser::ParseDirectiveSectionSwitch(const char *Segment,
891 const char *Section,
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000892 unsigned TAA, unsigned Align,
893 unsigned StubSize) {
Daniel Dunbar3f872332009-07-28 16:08:33 +0000894 if (Lexer.isNot(AsmToken::EndOfStatement))
Chris Lattner529fb542009-06-24 05:13:15 +0000895 return TokError("unexpected token in section switching directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +0000896 Lex();
Chris Lattner529fb542009-06-24 05:13:15 +0000897
Chris Lattner56594f92009-07-31 17:47:16 +0000898 // FIXME: Arch specific.
Chris Lattnerf60e9bb2010-02-26 18:32:26 +0000899 bool isText = StringRef(Segment) == "__TEXT"; // FIXME: Hack.
Chris Lattnerf0559e42010-04-08 20:30:37 +0000900 Out.SwitchSection(Ctx.getMachOSection(Segment, Section, TAA, StubSize,
901 isText ? SectionKind::getText()
902 : SectionKind::getDataRel()));
Daniel Dunbar2330df62009-08-21 23:30:15 +0000903
904 // Set the implicit alignment, if any.
905 //
906 // FIXME: This isn't really what 'as' does; I think it just uses the implicit
907 // alignment on the section (e.g., if one manually inserts bytes into the
908 // section, then just issueing the section switch directive will not realign
909 // the section. However, this is arguably more reasonable behavior, and there
910 // is no good reason for someone to intentionally emit incorrectly sized
911 // values into the implicitly aligned sections.
912 if (Align)
913 Out.EmitValueToAlignment(Align, 0, 1, 0);
914
Chris Lattner529fb542009-06-24 05:13:15 +0000915 return false;
916}
Daniel Dunbara0d14262009-06-24 23:30:00 +0000917
Daniel Dunbar1ab75942009-08-14 18:19:52 +0000918bool AsmParser::ParseEscapedString(std::string &Data) {
919 assert(Lexer.is(AsmToken::String) && "Unexpected current token!");
920
921 Data = "";
Sean Callanan18b83232010-01-19 21:44:56 +0000922 StringRef Str = getTok().getStringContents();
Daniel Dunbar1ab75942009-08-14 18:19:52 +0000923 for (unsigned i = 0, e = Str.size(); i != e; ++i) {
924 if (Str[i] != '\\') {
925 Data += Str[i];
926 continue;
927 }
928
929 // Recognize escaped characters. Note that this escape semantics currently
930 // loosely follows Darwin 'as'. Notably, it doesn't support hex escapes.
931 ++i;
932 if (i == e)
933 return TokError("unexpected backslash at end of string");
934
935 // Recognize octal sequences.
936 if ((unsigned) (Str[i] - '0') <= 7) {
937 // Consume up to three octal characters.
938 unsigned Value = Str[i] - '0';
939
940 if (i + 1 != e && ((unsigned) (Str[i + 1] - '0')) <= 7) {
941 ++i;
942 Value = Value * 8 + (Str[i] - '0');
943
944 if (i + 1 != e && ((unsigned) (Str[i + 1] - '0')) <= 7) {
945 ++i;
946 Value = Value * 8 + (Str[i] - '0');
947 }
948 }
949
950 if (Value > 255)
951 return TokError("invalid octal escape sequence (out of range)");
952
953 Data += (unsigned char) Value;
954 continue;
955 }
956
957 // Otherwise recognize individual escapes.
958 switch (Str[i]) {
959 default:
960 // Just reject invalid escape sequences for now.
961 return TokError("invalid escape sequence (unrecognized character)");
962
963 case 'b': Data += '\b'; break;
964 case 'f': Data += '\f'; break;
965 case 'n': Data += '\n'; break;
966 case 'r': Data += '\r'; break;
967 case 't': Data += '\t'; break;
968 case '"': Data += '"'; break;
969 case '\\': Data += '\\'; break;
970 }
971 }
972
973 return false;
974}
975
Daniel Dunbara0d14262009-06-24 23:30:00 +0000976/// ParseDirectiveAscii:
Daniel Dunbar475839e2009-06-29 20:37:27 +0000977/// ::= ( .ascii | .asciz ) [ "string" ( , "string" )* ]
Daniel Dunbara0d14262009-06-24 23:30:00 +0000978bool AsmParser::ParseDirectiveAscii(bool ZeroTerminated) {
Daniel Dunbar3f872332009-07-28 16:08:33 +0000979 if (Lexer.isNot(AsmToken::EndOfStatement)) {
Daniel Dunbara0d14262009-06-24 23:30:00 +0000980 for (;;) {
Daniel Dunbar3f872332009-07-28 16:08:33 +0000981 if (Lexer.isNot(AsmToken::String))
Daniel Dunbara0d14262009-06-24 23:30:00 +0000982 return TokError("expected string in '.ascii' or '.asciz' directive");
983
Daniel Dunbar1ab75942009-08-14 18:19:52 +0000984 std::string Data;
985 if (ParseEscapedString(Data))
986 return true;
987
Chris Lattneraaec2052010-01-19 19:46:13 +0000988 Out.EmitBytes(Data, DEFAULT_ADDRSPACE);
Daniel Dunbara0d14262009-06-24 23:30:00 +0000989 if (ZeroTerminated)
Chris Lattneraaec2052010-01-19 19:46:13 +0000990 Out.EmitBytes(StringRef("\0", 1), DEFAULT_ADDRSPACE);
Daniel Dunbara0d14262009-06-24 23:30:00 +0000991
Sean Callanan79ed1a82010-01-19 20:22:31 +0000992 Lex();
Daniel Dunbara0d14262009-06-24 23:30:00 +0000993
Daniel Dunbar3f872332009-07-28 16:08:33 +0000994 if (Lexer.is(AsmToken::EndOfStatement))
Daniel Dunbara0d14262009-06-24 23:30:00 +0000995 break;
996
Daniel Dunbar3f872332009-07-28 16:08:33 +0000997 if (Lexer.isNot(AsmToken::Comma))
Daniel Dunbara0d14262009-06-24 23:30:00 +0000998 return TokError("unexpected token in '.ascii' or '.asciz' directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +0000999 Lex();
Daniel Dunbara0d14262009-06-24 23:30:00 +00001000 }
1001 }
1002
Sean Callanan79ed1a82010-01-19 20:22:31 +00001003 Lex();
Daniel Dunbara0d14262009-06-24 23:30:00 +00001004 return false;
1005}
1006
1007/// ParseDirectiveValue
1008/// ::= (.byte | .short | ... ) [ expression (, expression)* ]
1009bool AsmParser::ParseDirectiveValue(unsigned Size) {
Daniel Dunbar3f872332009-07-28 16:08:33 +00001010 if (Lexer.isNot(AsmToken::EndOfStatement)) {
Daniel Dunbara0d14262009-06-24 23:30:00 +00001011 for (;;) {
Daniel Dunbar821e3332009-08-31 08:09:28 +00001012 const MCExpr *Value;
Bill Wendling9bc0af82009-12-28 01:34:57 +00001013 SMLoc ATTRIBUTE_UNUSED StartLoc = Lexer.getLoc();
Daniel Dunbar821e3332009-08-31 08:09:28 +00001014 if (ParseExpression(Value))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001015 return true;
1016
Chris Lattneraaec2052010-01-19 19:46:13 +00001017 Out.EmitValue(Value, Size, DEFAULT_ADDRSPACE);
Daniel Dunbara0d14262009-06-24 23:30:00 +00001018
Daniel Dunbar3f872332009-07-28 16:08:33 +00001019 if (Lexer.is(AsmToken::EndOfStatement))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001020 break;
1021
1022 // FIXME: Improve diagnostic.
Daniel Dunbar3f872332009-07-28 16:08:33 +00001023 if (Lexer.isNot(AsmToken::Comma))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001024 return TokError("unexpected token in directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001025 Lex();
Daniel Dunbara0d14262009-06-24 23:30:00 +00001026 }
1027 }
1028
Sean Callanan79ed1a82010-01-19 20:22:31 +00001029 Lex();
Daniel Dunbara0d14262009-06-24 23:30:00 +00001030 return false;
1031}
1032
1033/// ParseDirectiveSpace
1034/// ::= .space expression [ , expression ]
1035bool AsmParser::ParseDirectiveSpace() {
1036 int64_t NumBytes;
Daniel Dunbar475839e2009-06-29 20:37:27 +00001037 if (ParseAbsoluteExpression(NumBytes))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001038 return true;
1039
1040 int64_t FillExpr = 0;
1041 bool HasFillExpr = false;
Daniel Dunbar3f872332009-07-28 16:08:33 +00001042 if (Lexer.isNot(AsmToken::EndOfStatement)) {
1043 if (Lexer.isNot(AsmToken::Comma))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001044 return TokError("unexpected token in '.space' directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001045 Lex();
Daniel Dunbara0d14262009-06-24 23:30:00 +00001046
Daniel Dunbar475839e2009-06-29 20:37:27 +00001047 if (ParseAbsoluteExpression(FillExpr))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001048 return true;
1049
1050 HasFillExpr = true;
1051
Daniel Dunbar3f872332009-07-28 16:08:33 +00001052 if (Lexer.isNot(AsmToken::EndOfStatement))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001053 return TokError("unexpected token in '.space' directive");
1054 }
1055
Sean Callanan79ed1a82010-01-19 20:22:31 +00001056 Lex();
Daniel Dunbara0d14262009-06-24 23:30:00 +00001057
1058 if (NumBytes <= 0)
1059 return TokError("invalid number of bytes in '.space' directive");
1060
1061 // FIXME: Sometimes the fill expr is 'nop' if it isn't supplied, instead of 0.
Chris Lattneraaec2052010-01-19 19:46:13 +00001062 Out.EmitFill(NumBytes, FillExpr, DEFAULT_ADDRSPACE);
Daniel Dunbara0d14262009-06-24 23:30:00 +00001063
1064 return false;
1065}
1066
1067/// ParseDirectiveFill
1068/// ::= .fill expression , expression , expression
1069bool AsmParser::ParseDirectiveFill() {
1070 int64_t NumValues;
Daniel Dunbar475839e2009-06-29 20:37:27 +00001071 if (ParseAbsoluteExpression(NumValues))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001072 return true;
1073
Daniel Dunbar3f872332009-07-28 16:08:33 +00001074 if (Lexer.isNot(AsmToken::Comma))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001075 return TokError("unexpected token in '.fill' directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001076 Lex();
Daniel Dunbara0d14262009-06-24 23:30:00 +00001077
1078 int64_t FillSize;
Daniel Dunbar475839e2009-06-29 20:37:27 +00001079 if (ParseAbsoluteExpression(FillSize))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001080 return true;
1081
Daniel Dunbar3f872332009-07-28 16:08:33 +00001082 if (Lexer.isNot(AsmToken::Comma))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001083 return TokError("unexpected token in '.fill' directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001084 Lex();
Daniel Dunbara0d14262009-06-24 23:30:00 +00001085
1086 int64_t FillExpr;
Daniel Dunbar475839e2009-06-29 20:37:27 +00001087 if (ParseAbsoluteExpression(FillExpr))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001088 return true;
1089
Daniel Dunbar3f872332009-07-28 16:08:33 +00001090 if (Lexer.isNot(AsmToken::EndOfStatement))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001091 return TokError("unexpected token in '.fill' directive");
1092
Sean Callanan79ed1a82010-01-19 20:22:31 +00001093 Lex();
Daniel Dunbara0d14262009-06-24 23:30:00 +00001094
Daniel Dunbarbc38ca72009-08-21 15:43:35 +00001095 if (FillSize != 1 && FillSize != 2 && FillSize != 4 && FillSize != 8)
1096 return TokError("invalid '.fill' size, expected 1, 2, 4, or 8");
Daniel Dunbara0d14262009-06-24 23:30:00 +00001097
1098 for (uint64_t i = 0, e = NumValues; i != e; ++i)
Chris Lattneraaec2052010-01-19 19:46:13 +00001099 Out.EmitValue(MCConstantExpr::Create(FillExpr, getContext()), FillSize,
1100 DEFAULT_ADDRSPACE);
Daniel Dunbara0d14262009-06-24 23:30:00 +00001101
1102 return false;
1103}
Daniel Dunbarc238b582009-06-25 22:44:51 +00001104
1105/// ParseDirectiveOrg
1106/// ::= .org expression [ , expression ]
1107bool AsmParser::ParseDirectiveOrg() {
Daniel Dunbar821e3332009-08-31 08:09:28 +00001108 const MCExpr *Offset;
Daniel Dunbar883f9202009-08-31 08:08:50 +00001109 SMLoc StartLoc = Lexer.getLoc();
Daniel Dunbar821e3332009-08-31 08:09:28 +00001110 if (ParseExpression(Offset))
Daniel Dunbarc238b582009-06-25 22:44:51 +00001111 return true;
1112
1113 // Parse optional fill expression.
1114 int64_t FillExpr = 0;
Daniel Dunbar3f872332009-07-28 16:08:33 +00001115 if (Lexer.isNot(AsmToken::EndOfStatement)) {
1116 if (Lexer.isNot(AsmToken::Comma))
Daniel Dunbarc238b582009-06-25 22:44:51 +00001117 return TokError("unexpected token in '.org' directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001118 Lex();
Daniel Dunbarc238b582009-06-25 22:44:51 +00001119
Daniel Dunbar475839e2009-06-29 20:37:27 +00001120 if (ParseAbsoluteExpression(FillExpr))
Daniel Dunbarc238b582009-06-25 22:44:51 +00001121 return true;
1122
Daniel Dunbar3f872332009-07-28 16:08:33 +00001123 if (Lexer.isNot(AsmToken::EndOfStatement))
Daniel Dunbarc238b582009-06-25 22:44:51 +00001124 return TokError("unexpected token in '.org' directive");
1125 }
1126
Sean Callanan79ed1a82010-01-19 20:22:31 +00001127 Lex();
Daniel Dunbarf4b830f2009-06-30 02:10:03 +00001128
1129 // FIXME: Only limited forms of relocatable expressions are accepted here, it
1130 // has to be relative to the current section.
1131 Out.EmitValueToOffset(Offset, FillExpr);
Daniel Dunbarc238b582009-06-25 22:44:51 +00001132
1133 return false;
1134}
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001135
1136/// ParseDirectiveAlign
1137/// ::= {.align, ...} expression [ , expression [ , expression ]]
1138bool AsmParser::ParseDirectiveAlign(bool IsPow2, unsigned ValueSize) {
Daniel Dunbarb58a8042009-08-26 09:16:34 +00001139 SMLoc AlignmentLoc = Lexer.getLoc();
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001140 int64_t Alignment;
1141 if (ParseAbsoluteExpression(Alignment))
1142 return true;
1143
1144 SMLoc MaxBytesLoc;
1145 bool HasFillExpr = false;
1146 int64_t FillExpr = 0;
1147 int64_t MaxBytesToFill = 0;
Daniel Dunbar3f872332009-07-28 16:08:33 +00001148 if (Lexer.isNot(AsmToken::EndOfStatement)) {
1149 if (Lexer.isNot(AsmToken::Comma))
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001150 return TokError("unexpected token in directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001151 Lex();
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001152
1153 // The fill expression can be omitted while specifying a maximum number of
1154 // alignment bytes, e.g:
1155 // .align 3,,4
Daniel Dunbar3f872332009-07-28 16:08:33 +00001156 if (Lexer.isNot(AsmToken::Comma)) {
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001157 HasFillExpr = true;
1158 if (ParseAbsoluteExpression(FillExpr))
1159 return true;
1160 }
1161
Daniel Dunbar3f872332009-07-28 16:08:33 +00001162 if (Lexer.isNot(AsmToken::EndOfStatement)) {
1163 if (Lexer.isNot(AsmToken::Comma))
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001164 return TokError("unexpected token in directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001165 Lex();
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001166
1167 MaxBytesLoc = Lexer.getLoc();
1168 if (ParseAbsoluteExpression(MaxBytesToFill))
1169 return true;
1170
Daniel Dunbar3f872332009-07-28 16:08:33 +00001171 if (Lexer.isNot(AsmToken::EndOfStatement))
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001172 return TokError("unexpected token in directive");
1173 }
1174 }
1175
Sean Callanan79ed1a82010-01-19 20:22:31 +00001176 Lex();
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001177
1178 if (!HasFillExpr) {
1179 // FIXME: Sometimes fill with nop.
1180 FillExpr = 0;
1181 }
1182
1183 // Compute alignment in bytes.
1184 if (IsPow2) {
1185 // FIXME: Diagnose overflow.
Daniel Dunbarb58a8042009-08-26 09:16:34 +00001186 if (Alignment >= 32) {
1187 Error(AlignmentLoc, "invalid alignment value");
1188 Alignment = 31;
1189 }
1190
Benjamin Kramer12fd7672009-09-06 09:35:10 +00001191 Alignment = 1ULL << Alignment;
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001192 }
1193
Daniel Dunbarb58a8042009-08-26 09:16:34 +00001194 // Diagnose non-sensical max bytes to align.
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001195 if (MaxBytesLoc.isValid()) {
1196 if (MaxBytesToFill < 1) {
Daniel Dunbarb58a8042009-08-26 09:16:34 +00001197 Error(MaxBytesLoc, "alignment directive can never be satisfied in this "
1198 "many bytes, ignoring maximum bytes expression");
Daniel Dunbar0afb9f52009-08-21 23:01:53 +00001199 MaxBytesToFill = 0;
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001200 }
1201
1202 if (MaxBytesToFill >= Alignment) {
Daniel Dunbar3fb76832009-06-30 00:49:23 +00001203 Warning(MaxBytesLoc, "maximum bytes expression exceeds alignment and "
1204 "has no effect");
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001205 MaxBytesToFill = 0;
1206 }
1207 }
1208
Kevin Enderbyd74acb02010-02-25 18:46:04 +00001209 // FIXME: hard code the parser to use EmitCodeAlignment for text when using
1210 // the TextAlignFillValue.
1211 if(Out.getCurrentSection()->getKind().isText() &&
1212 Lexer.getMAI().getTextAlignFillValue() == FillExpr)
1213 Out.EmitCodeAlignment(Alignment, MaxBytesToFill);
1214 else
1215 // FIXME: Target specific behavior about how the "extra" bytes are filled.
1216 Out.EmitValueToAlignment(Alignment, FillExpr, ValueSize, MaxBytesToFill);
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001217
1218 return false;
1219}
1220
Daniel Dunbard7b267b2009-06-30 00:33:19 +00001221/// ParseDirectiveSymbolAttribute
1222/// ::= { ".globl", ".weak", ... } [ identifier ( , identifier )* ]
Chris Lattnera5ad93a2010-01-23 06:39:22 +00001223bool AsmParser::ParseDirectiveSymbolAttribute(MCSymbolAttr Attr) {
Daniel Dunbar3f872332009-07-28 16:08:33 +00001224 if (Lexer.isNot(AsmToken::EndOfStatement)) {
Daniel Dunbard7b267b2009-06-30 00:33:19 +00001225 for (;;) {
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00001226 StringRef Name;
1227
1228 if (ParseIdentifier(Name))
Daniel Dunbard7b267b2009-06-30 00:33:19 +00001229 return TokError("expected identifier in directive");
1230
Daniel Dunbar959fd882009-08-26 22:13:22 +00001231 MCSymbol *Sym = CreateSymbol(Name);
Daniel Dunbard7b267b2009-06-30 00:33:19 +00001232
Daniel Dunbard7b267b2009-06-30 00:33:19 +00001233 Out.EmitSymbolAttribute(Sym, Attr);
1234
Daniel Dunbar3f872332009-07-28 16:08:33 +00001235 if (Lexer.is(AsmToken::EndOfStatement))
Daniel Dunbard7b267b2009-06-30 00:33:19 +00001236 break;
1237
Daniel Dunbar3f872332009-07-28 16:08:33 +00001238 if (Lexer.isNot(AsmToken::Comma))
Daniel Dunbard7b267b2009-06-30 00:33:19 +00001239 return TokError("unexpected token in directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001240 Lex();
Daniel Dunbard7b267b2009-06-30 00:33:19 +00001241 }
1242 }
1243
Sean Callanan79ed1a82010-01-19 20:22:31 +00001244 Lex();
Daniel Dunbard7b267b2009-06-30 00:33:19 +00001245 return false;
1246}
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001247
Kevin Enderby95cf30c2009-07-14 18:17:10 +00001248/// ParseDirectiveDarwinSymbolDesc
1249/// ::= .desc identifier , expression
1250bool AsmParser::ParseDirectiveDarwinSymbolDesc() {
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00001251 StringRef Name;
1252 if (ParseIdentifier(Name))
Kevin Enderby95cf30c2009-07-14 18:17:10 +00001253 return TokError("expected identifier in directive");
1254
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00001255 // Handle the identifier as the key symbol.
Daniel Dunbar959fd882009-08-26 22:13:22 +00001256 MCSymbol *Sym = CreateSymbol(Name);
Kevin Enderby95cf30c2009-07-14 18:17:10 +00001257
Daniel Dunbar3f872332009-07-28 16:08:33 +00001258 if (Lexer.isNot(AsmToken::Comma))
Kevin Enderby95cf30c2009-07-14 18:17:10 +00001259 return TokError("unexpected token in '.desc' directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001260 Lex();
Kevin Enderby95cf30c2009-07-14 18:17:10 +00001261
1262 SMLoc DescLoc = Lexer.getLoc();
1263 int64_t DescValue;
1264 if (ParseAbsoluteExpression(DescValue))
1265 return true;
1266
Daniel Dunbar3f872332009-07-28 16:08:33 +00001267 if (Lexer.isNot(AsmToken::EndOfStatement))
Kevin Enderby95cf30c2009-07-14 18:17:10 +00001268 return TokError("unexpected token in '.desc' directive");
1269
Sean Callanan79ed1a82010-01-19 20:22:31 +00001270 Lex();
Kevin Enderby95cf30c2009-07-14 18:17:10 +00001271
1272 // Set the n_desc field of this Symbol to this DescValue
1273 Out.EmitSymbolDesc(Sym, DescValue);
1274
1275 return false;
1276}
1277
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001278/// ParseDirectiveComm
Chris Lattner1fc3d752009-07-09 17:25:12 +00001279/// ::= ( .comm | .lcomm ) identifier , size_expression [ , align_expression ]
1280bool AsmParser::ParseDirectiveComm(bool IsLocal) {
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00001281 SMLoc IDLoc = Lexer.getLoc();
1282 StringRef Name;
1283 if (ParseIdentifier(Name))
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001284 return TokError("expected identifier in directive");
1285
Daniel Dunbar76c4d762009-07-31 21:55:09 +00001286 // Handle the identifier as the key symbol.
Daniel Dunbar959fd882009-08-26 22:13:22 +00001287 MCSymbol *Sym = CreateSymbol(Name);
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001288
Daniel Dunbar3f872332009-07-28 16:08:33 +00001289 if (Lexer.isNot(AsmToken::Comma))
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001290 return TokError("unexpected token in directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001291 Lex();
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001292
1293 int64_t Size;
1294 SMLoc SizeLoc = Lexer.getLoc();
1295 if (ParseAbsoluteExpression(Size))
1296 return true;
1297
1298 int64_t Pow2Alignment = 0;
1299 SMLoc Pow2AlignmentLoc;
Daniel Dunbar3f872332009-07-28 16:08:33 +00001300 if (Lexer.is(AsmToken::Comma)) {
Sean Callanan79ed1a82010-01-19 20:22:31 +00001301 Lex();
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001302 Pow2AlignmentLoc = Lexer.getLoc();
1303 if (ParseAbsoluteExpression(Pow2Alignment))
1304 return true;
Chris Lattner258281d2010-01-19 06:22:22 +00001305
1306 // If this target takes alignments in bytes (not log) validate and convert.
1307 if (Lexer.getMAI().getAlignmentIsInBytes()) {
1308 if (!isPowerOf2_64(Pow2Alignment))
1309 return Error(Pow2AlignmentLoc, "alignment must be a power of 2");
1310 Pow2Alignment = Log2_64(Pow2Alignment);
1311 }
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001312 }
1313
Daniel Dunbar3f872332009-07-28 16:08:33 +00001314 if (Lexer.isNot(AsmToken::EndOfStatement))
Chris Lattner1fc3d752009-07-09 17:25:12 +00001315 return TokError("unexpected token in '.comm' or '.lcomm' directive");
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001316
Sean Callanan79ed1a82010-01-19 20:22:31 +00001317 Lex();
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001318
Chris Lattner1fc3d752009-07-09 17:25:12 +00001319 // NOTE: a size of zero for a .comm should create a undefined symbol
1320 // but a size of .lcomm creates a bss symbol of size zero.
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001321 if (Size < 0)
Chris Lattner1fc3d752009-07-09 17:25:12 +00001322 return Error(SizeLoc, "invalid '.comm' or '.lcomm' directive size, can't "
1323 "be less than zero");
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001324
1325 // NOTE: The alignment in the directive is a power of 2 value, the assember
1326 // may internally end up wanting an alignment in bytes.
1327 // FIXME: Diagnose overflow.
1328 if (Pow2Alignment < 0)
Chris Lattner1fc3d752009-07-09 17:25:12 +00001329 return Error(Pow2AlignmentLoc, "invalid '.comm' or '.lcomm' directive "
1330 "alignment, can't be less than zero");
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001331
Daniel Dunbar8906ff12009-08-22 07:22:36 +00001332 if (!Sym->isUndefined())
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001333 return Error(IDLoc, "invalid symbol redefinition");
1334
Daniel Dunbar7092c7e2009-08-30 06:17:16 +00001335 // '.lcomm' is equivalent to '.zerofill'.
Chris Lattner1fc3d752009-07-09 17:25:12 +00001336 // Create the Symbol as a common or local common with Size and Pow2Alignment
Daniel Dunbar7092c7e2009-08-30 06:17:16 +00001337 if (IsLocal) {
Chris Lattnerf0559e42010-04-08 20:30:37 +00001338 Out.EmitZerofill(Ctx.getMachOSection("__DATA", "__bss",
1339 MCSectionMachO::S_ZEROFILL, 0,
1340 SectionKind::getBSS()),
Daniel Dunbar7092c7e2009-08-30 06:17:16 +00001341 Sym, Size, 1 << Pow2Alignment);
1342 return false;
1343 }
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001344
Daniel Dunbar7092c7e2009-08-30 06:17:16 +00001345 Out.EmitCommonSymbol(Sym, Size, 1 << Pow2Alignment);
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001346 return false;
1347}
Chris Lattner9be3fee2009-07-10 22:20:30 +00001348
1349/// ParseDirectiveDarwinZerofill
1350/// ::= .zerofill segname , sectname [, identifier , size_expression [
1351/// , align_expression ]]
1352bool AsmParser::ParseDirectiveDarwinZerofill() {
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00001353 // FIXME: Handle quoted names here.
1354
Daniel Dunbar3f872332009-07-28 16:08:33 +00001355 if (Lexer.isNot(AsmToken::Identifier))
Chris Lattner9be3fee2009-07-10 22:20:30 +00001356 return TokError("expected segment name after '.zerofill' directive");
Sean Callanan18b83232010-01-19 21:44:56 +00001357 StringRef Segment = getTok().getString();
Sean Callanan79ed1a82010-01-19 20:22:31 +00001358 Lex();
Chris Lattner9be3fee2009-07-10 22:20:30 +00001359
Daniel Dunbar3f872332009-07-28 16:08:33 +00001360 if (Lexer.isNot(AsmToken::Comma))
Chris Lattner9be3fee2009-07-10 22:20:30 +00001361 return TokError("unexpected token in directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001362 Lex();
Chris Lattner9be3fee2009-07-10 22:20:30 +00001363
Daniel Dunbar3f872332009-07-28 16:08:33 +00001364 if (Lexer.isNot(AsmToken::Identifier))
Chris Lattner9be3fee2009-07-10 22:20:30 +00001365 return TokError("expected section name after comma in '.zerofill' "
1366 "directive");
Sean Callanan18b83232010-01-19 21:44:56 +00001367 StringRef Section = getTok().getString();
Sean Callanan79ed1a82010-01-19 20:22:31 +00001368 Lex();
Chris Lattner9be3fee2009-07-10 22:20:30 +00001369
Chris Lattner9be3fee2009-07-10 22:20:30 +00001370 // If this is the end of the line all that was wanted was to create the
1371 // the section but with no symbol.
Daniel Dunbar3f872332009-07-28 16:08:33 +00001372 if (Lexer.is(AsmToken::EndOfStatement)) {
Chris Lattner9be3fee2009-07-10 22:20:30 +00001373 // Create the zerofill section but no symbol
Chris Lattnerf0559e42010-04-08 20:30:37 +00001374 Out.EmitZerofill(Ctx.getMachOSection(Segment, Section,
1375 MCSectionMachO::S_ZEROFILL, 0,
1376 SectionKind::getBSS()));
Chris Lattner9be3fee2009-07-10 22:20:30 +00001377 return false;
1378 }
1379
Daniel Dunbar3f872332009-07-28 16:08:33 +00001380 if (Lexer.isNot(AsmToken::Comma))
Chris Lattner9be3fee2009-07-10 22:20:30 +00001381 return TokError("unexpected token in directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001382 Lex();
Chris Lattner9be3fee2009-07-10 22:20:30 +00001383
Daniel Dunbar3f872332009-07-28 16:08:33 +00001384 if (Lexer.isNot(AsmToken::Identifier))
Chris Lattner9be3fee2009-07-10 22:20:30 +00001385 return TokError("expected identifier in directive");
1386
1387 // handle the identifier as the key symbol.
1388 SMLoc IDLoc = Lexer.getLoc();
Sean Callanan18b83232010-01-19 21:44:56 +00001389 MCSymbol *Sym = CreateSymbol(getTok().getString());
Sean Callanan79ed1a82010-01-19 20:22:31 +00001390 Lex();
Chris Lattner9be3fee2009-07-10 22:20:30 +00001391
Daniel Dunbar3f872332009-07-28 16:08:33 +00001392 if (Lexer.isNot(AsmToken::Comma))
Chris Lattner9be3fee2009-07-10 22:20:30 +00001393 return TokError("unexpected token in directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001394 Lex();
Chris Lattner9be3fee2009-07-10 22:20:30 +00001395
1396 int64_t Size;
1397 SMLoc SizeLoc = Lexer.getLoc();
1398 if (ParseAbsoluteExpression(Size))
1399 return true;
1400
1401 int64_t Pow2Alignment = 0;
1402 SMLoc Pow2AlignmentLoc;
Daniel Dunbar3f872332009-07-28 16:08:33 +00001403 if (Lexer.is(AsmToken::Comma)) {
Sean Callanan79ed1a82010-01-19 20:22:31 +00001404 Lex();
Chris Lattner9be3fee2009-07-10 22:20:30 +00001405 Pow2AlignmentLoc = Lexer.getLoc();
1406 if (ParseAbsoluteExpression(Pow2Alignment))
1407 return true;
1408 }
1409
Daniel Dunbar3f872332009-07-28 16:08:33 +00001410 if (Lexer.isNot(AsmToken::EndOfStatement))
Chris Lattner9be3fee2009-07-10 22:20:30 +00001411 return TokError("unexpected token in '.zerofill' directive");
1412
Sean Callanan79ed1a82010-01-19 20:22:31 +00001413 Lex();
Chris Lattner9be3fee2009-07-10 22:20:30 +00001414
1415 if (Size < 0)
1416 return Error(SizeLoc, "invalid '.zerofill' directive size, can't be less "
1417 "than zero");
1418
1419 // NOTE: The alignment in the directive is a power of 2 value, the assember
1420 // may internally end up wanting an alignment in bytes.
1421 // FIXME: Diagnose overflow.
1422 if (Pow2Alignment < 0)
1423 return Error(Pow2AlignmentLoc, "invalid '.zerofill' directive alignment, "
1424 "can't be less than zero");
1425
Daniel Dunbar8906ff12009-08-22 07:22:36 +00001426 if (!Sym->isUndefined())
Chris Lattner9be3fee2009-07-10 22:20:30 +00001427 return Error(IDLoc, "invalid symbol redefinition");
1428
Daniel Dunbarbdee6df2009-08-27 23:58:10 +00001429 // Create the zerofill Symbol with Size and Pow2Alignment
Daniel Dunbar2e152922009-08-28 05:48:29 +00001430 //
1431 // FIXME: Arch specific.
Chris Lattnerf0559e42010-04-08 20:30:37 +00001432 Out.EmitZerofill(Ctx.getMachOSection(Segment, Section,
1433 MCSectionMachO::S_ZEROFILL, 0,
1434 SectionKind::getBSS()),
Daniel Dunbar7092c7e2009-08-30 06:17:16 +00001435 Sym, Size, 1 << Pow2Alignment);
Chris Lattner9be3fee2009-07-10 22:20:30 +00001436
1437 return false;
1438}
Kevin Enderbya5c78322009-07-13 21:03:15 +00001439
1440/// ParseDirectiveDarwinSubsectionsViaSymbols
1441/// ::= .subsections_via_symbols
1442bool AsmParser::ParseDirectiveDarwinSubsectionsViaSymbols() {
Daniel Dunbar3f872332009-07-28 16:08:33 +00001443 if (Lexer.isNot(AsmToken::EndOfStatement))
Kevin Enderbya5c78322009-07-13 21:03:15 +00001444 return TokError("unexpected token in '.subsections_via_symbols' directive");
1445
Sean Callanan79ed1a82010-01-19 20:22:31 +00001446 Lex();
Kevin Enderbya5c78322009-07-13 21:03:15 +00001447
Chris Lattnera5ad93a2010-01-23 06:39:22 +00001448 Out.EmitAssemblerFlag(MCAF_SubsectionsViaSymbols);
Kevin Enderbya5c78322009-07-13 21:03:15 +00001449
1450 return false;
1451}
Kevin Enderby5f1f0b82009-07-13 23:15:14 +00001452
1453/// ParseDirectiveAbort
1454/// ::= .abort [ "abort_string" ]
1455bool AsmParser::ParseDirectiveAbort() {
Daniel Dunbarf9507ff2009-07-27 23:20:52 +00001456 // FIXME: Use loc from directive.
1457 SMLoc Loc = Lexer.getLoc();
1458
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001459 StringRef Str = "";
Daniel Dunbar3f872332009-07-28 16:08:33 +00001460 if (Lexer.isNot(AsmToken::EndOfStatement)) {
1461 if (Lexer.isNot(AsmToken::String))
Kevin Enderby5f1f0b82009-07-13 23:15:14 +00001462 return TokError("expected string in '.abort' directive");
1463
Sean Callanan18b83232010-01-19 21:44:56 +00001464 Str = getTok().getString();
Kevin Enderby5f1f0b82009-07-13 23:15:14 +00001465
Sean Callanan79ed1a82010-01-19 20:22:31 +00001466 Lex();
Kevin Enderby5f1f0b82009-07-13 23:15:14 +00001467 }
1468
Daniel Dunbar3f872332009-07-28 16:08:33 +00001469 if (Lexer.isNot(AsmToken::EndOfStatement))
Kevin Enderby5f1f0b82009-07-13 23:15:14 +00001470 return TokError("unexpected token in '.abort' directive");
1471
Sean Callanan79ed1a82010-01-19 20:22:31 +00001472 Lex();
Kevin Enderby5f1f0b82009-07-13 23:15:14 +00001473
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001474 // FIXME: Handle here.
Daniel Dunbarf9507ff2009-07-27 23:20:52 +00001475 if (Str.empty())
1476 Error(Loc, ".abort detected. Assembly stopping.");
1477 else
1478 Error(Loc, ".abort '" + Str + "' detected. Assembly stopping.");
Kevin Enderby5f1f0b82009-07-13 23:15:14 +00001479
1480 return false;
1481}
Kevin Enderby71148242009-07-14 21:35:03 +00001482
1483/// ParseDirectiveLsym
1484/// ::= .lsym identifier , expression
1485bool AsmParser::ParseDirectiveDarwinLsym() {
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00001486 StringRef Name;
1487 if (ParseIdentifier(Name))
Kevin Enderby71148242009-07-14 21:35:03 +00001488 return TokError("expected identifier in directive");
1489
Daniel Dunbar76c4d762009-07-31 21:55:09 +00001490 // Handle the identifier as the key symbol.
Daniel Dunbar959fd882009-08-26 22:13:22 +00001491 MCSymbol *Sym = CreateSymbol(Name);
Kevin Enderby71148242009-07-14 21:35:03 +00001492
Daniel Dunbar3f872332009-07-28 16:08:33 +00001493 if (Lexer.isNot(AsmToken::Comma))
Kevin Enderby71148242009-07-14 21:35:03 +00001494 return TokError("unexpected token in '.lsym' directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001495 Lex();
Kevin Enderby71148242009-07-14 21:35:03 +00001496
Daniel Dunbar821e3332009-08-31 08:09:28 +00001497 const MCExpr *Value;
Daniel Dunbar883f9202009-08-31 08:08:50 +00001498 SMLoc StartLoc = Lexer.getLoc();
Daniel Dunbar821e3332009-08-31 08:09:28 +00001499 if (ParseExpression(Value))
Kevin Enderby71148242009-07-14 21:35:03 +00001500 return true;
1501
Daniel Dunbar3f872332009-07-28 16:08:33 +00001502 if (Lexer.isNot(AsmToken::EndOfStatement))
Kevin Enderby71148242009-07-14 21:35:03 +00001503 return TokError("unexpected token in '.lsym' directive");
1504
Sean Callanan79ed1a82010-01-19 20:22:31 +00001505 Lex();
Kevin Enderby71148242009-07-14 21:35:03 +00001506
Daniel Dunbar7092c7e2009-08-30 06:17:16 +00001507 // We don't currently support this directive.
1508 //
1509 // FIXME: Diagnostic location!
1510 (void) Sym;
1511 return TokError("directive '.lsym' is unsupported");
Kevin Enderby71148242009-07-14 21:35:03 +00001512}
Kevin Enderby1f049b22009-07-14 23:21:55 +00001513
1514/// ParseDirectiveInclude
1515/// ::= .include "filename"
1516bool AsmParser::ParseDirectiveInclude() {
Daniel Dunbar3f872332009-07-28 16:08:33 +00001517 if (Lexer.isNot(AsmToken::String))
Kevin Enderby1f049b22009-07-14 23:21:55 +00001518 return TokError("expected string in '.include' directive");
1519
Sean Callanan18b83232010-01-19 21:44:56 +00001520 std::string Filename = getTok().getString();
Chris Lattner8e25e2d2009-07-16 06:14:39 +00001521 SMLoc IncludeLoc = Lexer.getLoc();
Sean Callanan79ed1a82010-01-19 20:22:31 +00001522 Lex();
Kevin Enderby1f049b22009-07-14 23:21:55 +00001523
Daniel Dunbar3f872332009-07-28 16:08:33 +00001524 if (Lexer.isNot(AsmToken::EndOfStatement))
Kevin Enderby1f049b22009-07-14 23:21:55 +00001525 return TokError("unexpected token in '.include' directive");
1526
Chris Lattner8e25e2d2009-07-16 06:14:39 +00001527 // Strip the quotes.
1528 Filename = Filename.substr(1, Filename.size()-2);
1529
1530 // Attempt to switch the lexer to the included file before consuming the end
1531 // of statement to avoid losing it when we switch.
Sean Callananfd0b0282010-01-21 00:19:58 +00001532 if (EnterIncludeFile(Filename)) {
Sean Callananbf2013e2010-01-20 23:19:55 +00001533 PrintMessage(IncludeLoc,
1534 "Could not find include file '" + Filename + "'",
1535 "error");
Chris Lattner8e25e2d2009-07-16 06:14:39 +00001536 return true;
1537 }
Kevin Enderby1f049b22009-07-14 23:21:55 +00001538
1539 return false;
1540}
Kevin Enderby6e68cd92009-07-15 15:30:11 +00001541
1542/// ParseDirectiveDarwinDumpOrLoad
1543/// ::= ( .dump | .load ) "filename"
Kevin Enderby5026ae42009-07-20 20:25:37 +00001544bool AsmParser::ParseDirectiveDarwinDumpOrLoad(SMLoc IDLoc, bool IsDump) {
Daniel Dunbar3f872332009-07-28 16:08:33 +00001545 if (Lexer.isNot(AsmToken::String))
Kevin Enderby6e68cd92009-07-15 15:30:11 +00001546 return TokError("expected string in '.dump' or '.load' directive");
1547
Sean Callanan79ed1a82010-01-19 20:22:31 +00001548 Lex();
Kevin Enderby6e68cd92009-07-15 15:30:11 +00001549
Daniel Dunbar3f872332009-07-28 16:08:33 +00001550 if (Lexer.isNot(AsmToken::EndOfStatement))
Kevin Enderby6e68cd92009-07-15 15:30:11 +00001551 return TokError("unexpected token in '.dump' or '.load' directive");
1552
Sean Callanan79ed1a82010-01-19 20:22:31 +00001553 Lex();
Kevin Enderby6e68cd92009-07-15 15:30:11 +00001554
Kevin Enderby5026ae42009-07-20 20:25:37 +00001555 // FIXME: If/when .dump and .load are implemented they will be done in the
1556 // the assembly parser and not have any need for an MCStreamer API.
Kevin Enderby6e68cd92009-07-15 15:30:11 +00001557 if (IsDump)
Kevin Enderby5026ae42009-07-20 20:25:37 +00001558 Warning(IDLoc, "ignoring directive .dump for now");
Kevin Enderby6e68cd92009-07-15 15:30:11 +00001559 else
Kevin Enderby5026ae42009-07-20 20:25:37 +00001560 Warning(IDLoc, "ignoring directive .load for now");
Kevin Enderby6e68cd92009-07-15 15:30:11 +00001561
1562 return false;
1563}
Kevin Enderbyc114ed72009-08-07 22:46:00 +00001564
1565/// ParseDirectiveIf
1566/// ::= .if expression
1567bool AsmParser::ParseDirectiveIf(SMLoc DirectiveLoc) {
1568 // Consume the identifier that was the .if directive
Sean Callanan79ed1a82010-01-19 20:22:31 +00001569 Lex();
Kevin Enderbyc114ed72009-08-07 22:46:00 +00001570
1571 TheCondStack.push_back(TheCondState);
1572 TheCondState.TheCond = AsmCond::IfCond;
1573 if(TheCondState.Ignore) {
1574 EatToEndOfStatement();
1575 }
1576 else {
1577 int64_t ExprValue;
1578 if (ParseAbsoluteExpression(ExprValue))
1579 return true;
1580
1581 if (Lexer.isNot(AsmToken::EndOfStatement))
1582 return TokError("unexpected token in '.if' directive");
1583
Sean Callanan79ed1a82010-01-19 20:22:31 +00001584 Lex();
Kevin Enderbyc114ed72009-08-07 22:46:00 +00001585
1586 TheCondState.CondMet = ExprValue;
1587 TheCondState.Ignore = !TheCondState.CondMet;
1588 }
1589
1590 return false;
1591}
1592
1593/// ParseDirectiveElseIf
1594/// ::= .elseif expression
1595bool AsmParser::ParseDirectiveElseIf(SMLoc DirectiveLoc) {
1596 if (TheCondState.TheCond != AsmCond::IfCond &&
1597 TheCondState.TheCond != AsmCond::ElseIfCond)
1598 Error(DirectiveLoc, "Encountered a .elseif that doesn't follow a .if or "
1599 " an .elseif");
1600 TheCondState.TheCond = AsmCond::ElseIfCond;
1601
1602 // Consume the identifier that was the .elseif directive
Sean Callanan79ed1a82010-01-19 20:22:31 +00001603 Lex();
Kevin Enderbyc114ed72009-08-07 22:46:00 +00001604
1605 bool LastIgnoreState = false;
1606 if (!TheCondStack.empty())
1607 LastIgnoreState = TheCondStack.back().Ignore;
1608 if (LastIgnoreState || TheCondState.CondMet) {
1609 TheCondState.Ignore = true;
1610 EatToEndOfStatement();
1611 }
1612 else {
1613 int64_t ExprValue;
1614 if (ParseAbsoluteExpression(ExprValue))
1615 return true;
1616
1617 if (Lexer.isNot(AsmToken::EndOfStatement))
1618 return TokError("unexpected token in '.elseif' directive");
1619
Sean Callanan79ed1a82010-01-19 20:22:31 +00001620 Lex();
Kevin Enderbyc114ed72009-08-07 22:46:00 +00001621 TheCondState.CondMet = ExprValue;
1622 TheCondState.Ignore = !TheCondState.CondMet;
1623 }
1624
1625 return false;
1626}
1627
1628/// ParseDirectiveElse
1629/// ::= .else
1630bool AsmParser::ParseDirectiveElse(SMLoc DirectiveLoc) {
1631 // Consume the identifier that was the .else directive
Sean Callanan79ed1a82010-01-19 20:22:31 +00001632 Lex();
Kevin Enderbyc114ed72009-08-07 22:46:00 +00001633
1634 if (Lexer.isNot(AsmToken::EndOfStatement))
1635 return TokError("unexpected token in '.else' directive");
1636
Sean Callanan79ed1a82010-01-19 20:22:31 +00001637 Lex();
Kevin Enderbyc114ed72009-08-07 22:46:00 +00001638
1639 if (TheCondState.TheCond != AsmCond::IfCond &&
1640 TheCondState.TheCond != AsmCond::ElseIfCond)
1641 Error(DirectiveLoc, "Encountered a .else that doesn't follow a .if or an "
1642 ".elseif");
1643 TheCondState.TheCond = AsmCond::ElseCond;
1644 bool LastIgnoreState = false;
1645 if (!TheCondStack.empty())
1646 LastIgnoreState = TheCondStack.back().Ignore;
1647 if (LastIgnoreState || TheCondState.CondMet)
1648 TheCondState.Ignore = true;
1649 else
1650 TheCondState.Ignore = false;
1651
1652 return false;
1653}
1654
1655/// ParseDirectiveEndIf
1656/// ::= .endif
1657bool AsmParser::ParseDirectiveEndIf(SMLoc DirectiveLoc) {
1658 // Consume the identifier that was the .endif directive
Sean Callanan79ed1a82010-01-19 20:22:31 +00001659 Lex();
Kevin Enderbyc114ed72009-08-07 22:46:00 +00001660
1661 if (Lexer.isNot(AsmToken::EndOfStatement))
1662 return TokError("unexpected token in '.endif' directive");
1663
Sean Callanan79ed1a82010-01-19 20:22:31 +00001664 Lex();
Kevin Enderbyc114ed72009-08-07 22:46:00 +00001665
1666 if ((TheCondState.TheCond == AsmCond::NoCond) ||
1667 TheCondStack.empty())
1668 Error(DirectiveLoc, "Encountered a .endif that doesn't follow a .if or "
1669 ".else");
1670 if (!TheCondStack.empty()) {
1671 TheCondState = TheCondStack.back();
1672 TheCondStack.pop_back();
1673 }
1674
1675 return false;
1676}
Daniel Dunbard0c14d62009-08-11 04:24:50 +00001677
1678/// ParseDirectiveFile
1679/// ::= .file [number] string
Chris Lattnerebb89b42009-09-27 21:16:52 +00001680bool AsmParser::ParseDirectiveFile(StringRef, SMLoc DirectiveLoc) {
Daniel Dunbard0c14d62009-08-11 04:24:50 +00001681 // FIXME: I'm not sure what this is.
1682 int64_t FileNumber = -1;
1683 if (Lexer.is(AsmToken::Integer)) {
Sean Callanan18b83232010-01-19 21:44:56 +00001684 FileNumber = getTok().getIntVal();
Sean Callanan79ed1a82010-01-19 20:22:31 +00001685 Lex();
Daniel Dunbard0c14d62009-08-11 04:24:50 +00001686
1687 if (FileNumber < 1)
1688 return TokError("file number less than one");
1689 }
1690
1691 if (Lexer.isNot(AsmToken::String))
1692 return TokError("unexpected token in '.file' directive");
1693
Chris Lattnerd32e8032010-01-25 19:02:58 +00001694 StringRef Filename = getTok().getString();
1695 Filename = Filename.substr(1, Filename.size()-2);
Sean Callanan79ed1a82010-01-19 20:22:31 +00001696 Lex();
Daniel Dunbard0c14d62009-08-11 04:24:50 +00001697
1698 if (Lexer.isNot(AsmToken::EndOfStatement))
1699 return TokError("unexpected token in '.file' directive");
1700
Chris Lattnerd32e8032010-01-25 19:02:58 +00001701 if (FileNumber == -1)
1702 Out.EmitFileDirective(Filename);
1703 else
1704 Out.EmitDwarfFileDirective(FileNumber, Filename);
1705
Daniel Dunbard0c14d62009-08-11 04:24:50 +00001706 return false;
1707}
1708
1709/// ParseDirectiveLine
1710/// ::= .line [number]
Chris Lattnerebb89b42009-09-27 21:16:52 +00001711bool AsmParser::ParseDirectiveLine(StringRef, SMLoc DirectiveLoc) {
Daniel Dunbard0c14d62009-08-11 04:24:50 +00001712 if (Lexer.isNot(AsmToken::EndOfStatement)) {
1713 if (Lexer.isNot(AsmToken::Integer))
1714 return TokError("unexpected token in '.line' directive");
1715
Sean Callanan18b83232010-01-19 21:44:56 +00001716 int64_t LineNumber = getTok().getIntVal();
Daniel Dunbard0c14d62009-08-11 04:24:50 +00001717 (void) LineNumber;
Sean Callanan79ed1a82010-01-19 20:22:31 +00001718 Lex();
Daniel Dunbard0c14d62009-08-11 04:24:50 +00001719
1720 // FIXME: Do something with the .line.
1721 }
1722
1723 if (Lexer.isNot(AsmToken::EndOfStatement))
1724 return TokError("unexpected token in '.file' directive");
1725
1726 return false;
1727}
1728
1729
1730/// ParseDirectiveLoc
1731/// ::= .loc number [number [number]]
Chris Lattnerebb89b42009-09-27 21:16:52 +00001732bool AsmParser::ParseDirectiveLoc(StringRef, SMLoc DirectiveLoc) {
Daniel Dunbard0c14d62009-08-11 04:24:50 +00001733 if (Lexer.isNot(AsmToken::Integer))
1734 return TokError("unexpected token in '.loc' directive");
1735
1736 // FIXME: What are these fields?
Sean Callanan18b83232010-01-19 21:44:56 +00001737 int64_t FileNumber = getTok().getIntVal();
Daniel Dunbard0c14d62009-08-11 04:24:50 +00001738 (void) FileNumber;
1739 // FIXME: Validate file.
1740
Sean Callanan79ed1a82010-01-19 20:22:31 +00001741 Lex();
Daniel Dunbard0c14d62009-08-11 04:24:50 +00001742 if (Lexer.isNot(AsmToken::EndOfStatement)) {
1743 if (Lexer.isNot(AsmToken::Integer))
1744 return TokError("unexpected token in '.loc' directive");
1745
Sean Callanan18b83232010-01-19 21:44:56 +00001746 int64_t Param2 = getTok().getIntVal();
Daniel Dunbard0c14d62009-08-11 04:24:50 +00001747 (void) Param2;
Sean Callanan79ed1a82010-01-19 20:22:31 +00001748 Lex();
Daniel Dunbard0c14d62009-08-11 04:24:50 +00001749
1750 if (Lexer.isNot(AsmToken::EndOfStatement)) {
1751 if (Lexer.isNot(AsmToken::Integer))
1752 return TokError("unexpected token in '.loc' directive");
1753
Sean Callanan18b83232010-01-19 21:44:56 +00001754 int64_t Param3 = getTok().getIntVal();
Daniel Dunbard0c14d62009-08-11 04:24:50 +00001755 (void) Param3;
Sean Callanan79ed1a82010-01-19 20:22:31 +00001756 Lex();
Daniel Dunbard0c14d62009-08-11 04:24:50 +00001757
1758 // FIXME: Do something with the .loc.
1759 }
1760 }
1761
1762 if (Lexer.isNot(AsmToken::EndOfStatement))
1763 return TokError("unexpected token in '.file' directive");
1764
1765 return false;
1766}
1767