blob: 9db0ac0351b69b2f827ac0c963c46f11c3937e9f [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
14#include "AsmParser.h"
Daniel Dunbar475839e2009-06-29 20:37:27 +000015
16#include "AsmExpr.h"
Daniel Dunbarf9507ff2009-07-27 23:20:52 +000017#include "llvm/ADT/Twine.h"
Daniel Dunbarecc63f82009-06-23 22:01:43 +000018#include "llvm/MC/MCContext.h"
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 Lattnerb0789ed2009-06-21 20:54:55 +000023#include "llvm/Support/SourceMgr.h"
24#include "llvm/Support/raw_ostream.h"
Daniel Dunbara3af3702009-07-20 18:55:04 +000025#include "llvm/Target/TargetAsmParser.h"
Chris Lattner27aa7d22009-06-21 20:16:42 +000026using namespace llvm;
27
Daniel Dunbarf9507ff2009-07-27 23:20:52 +000028void AsmParser::Warning(SMLoc L, const Twine &Msg) {
29 Lexer.PrintMessage(L, Msg.str(), "warning");
Daniel Dunbar3fb76832009-06-30 00:49:23 +000030}
31
Daniel Dunbarf9507ff2009-07-27 23:20:52 +000032bool AsmParser::Error(SMLoc L, const Twine &Msg) {
33 Lexer.PrintMessage(L, Msg.str(), "error");
Chris Lattner14ee48a2009-06-21 21:22:11 +000034 return true;
35}
36
37bool AsmParser::TokError(const char *Msg) {
Daniel Dunbar3fb76832009-06-30 00:49:23 +000038 Lexer.PrintMessage(Lexer.getLoc(), Msg, "error");
Chris Lattner14ee48a2009-06-21 21:22:11 +000039 return true;
40}
41
Chris Lattner27aa7d22009-06-21 20:16:42 +000042bool AsmParser::Run() {
Chris Lattnerb0789ed2009-06-21 20:54:55 +000043 // Prime the lexer.
44 Lexer.Lex();
45
Chris Lattnerb717fb02009-07-02 21:53:43 +000046 bool HadError = false;
Chris Lattnerb0789ed2009-06-21 20:54:55 +000047
Kevin Enderbyc114ed72009-08-07 22:46:00 +000048 AsmCond StartingCondState = TheCondState;
49
Chris Lattnerb717fb02009-07-02 21:53:43 +000050 // While we have input, parse each statement.
Daniel Dunbar3f872332009-07-28 16:08:33 +000051 while (Lexer.isNot(AsmToken::Eof)) {
Kevin Enderbyc114ed72009-08-07 22:46:00 +000052 // Handle conditional assembly here before calling ParseStatement()
53 if (Lexer.getKind() == AsmToken::Identifier) {
54 // If we have an identifier, handle it as the key symbol.
55 AsmToken ID = Lexer.getTok();
56 SMLoc IDLoc = ID.getLoc();
57 StringRef IDVal = ID.getString();
58
59 if (IDVal == ".if" ||
60 IDVal == ".elseif" ||
61 IDVal == ".else" ||
62 IDVal == ".endif") {
63 if (!ParseConditionalAssemblyDirectives(IDVal, IDLoc))
64 continue;
65 HadError = true;
66 EatToEndOfStatement();
67 continue;
68 }
69 }
70 if (TheCondState.Ignore) {
71 EatToEndOfStatement();
72 continue;
73 }
74
Chris Lattnerb717fb02009-07-02 21:53:43 +000075 if (!ParseStatement()) continue;
76
Kevin Enderbyc114ed72009-08-07 22:46:00 +000077 // We had an error, remember it and recover by skipping to the next line.
Chris Lattnerb717fb02009-07-02 21:53:43 +000078 HadError = true;
79 EatToEndOfStatement();
80 }
Kevin Enderbyc114ed72009-08-07 22:46:00 +000081
82 if (TheCondState.TheCond != StartingCondState.TheCond ||
83 TheCondState.Ignore != StartingCondState.Ignore)
84 return TokError("unmatched .ifs or .elses");
Chris Lattnerb717fb02009-07-02 21:53:43 +000085
Daniel Dunbarb3f3c032009-08-21 08:34:18 +000086 if (!HadError)
87 Out.Finish();
88
Chris Lattnerb717fb02009-07-02 21:53:43 +000089 return HadError;
Chris Lattnerb0789ed2009-06-21 20:54:55 +000090}
91
Kevin Enderbyc114ed72009-08-07 22:46:00 +000092/// ParseConditionalAssemblyDirectives - parse the conditional assembly
93/// directives
94bool AsmParser::ParseConditionalAssemblyDirectives(StringRef Directive,
95 SMLoc DirectiveLoc) {
96 if (Directive == ".if")
97 return ParseDirectiveIf(DirectiveLoc);
98 if (Directive == ".elseif")
99 return ParseDirectiveElseIf(DirectiveLoc);
100 if (Directive == ".else")
101 return ParseDirectiveElse(DirectiveLoc);
102 if (Directive == ".endif")
103 return ParseDirectiveEndIf(DirectiveLoc);
104 return true;
105}
106
Chris Lattner2cf5f142009-06-22 01:29:09 +0000107/// EatToEndOfStatement - Throw away the rest of the line for testing purposes.
108void AsmParser::EatToEndOfStatement() {
Daniel Dunbar3f872332009-07-28 16:08:33 +0000109 while (Lexer.isNot(AsmToken::EndOfStatement) &&
110 Lexer.isNot(AsmToken::Eof))
Chris Lattner2cf5f142009-06-22 01:29:09 +0000111 Lexer.Lex();
112
113 // Eat EOL.
Daniel Dunbar3f872332009-07-28 16:08:33 +0000114 if (Lexer.is(AsmToken::EndOfStatement))
Chris Lattner2cf5f142009-06-22 01:29:09 +0000115 Lexer.Lex();
116}
117
Chris Lattnerc4193832009-06-22 05:51:26 +0000118
Chris Lattner74ec1a32009-06-22 06:32:03 +0000119/// ParseParenExpr - Parse a paren expression and return it.
120/// NOTE: This assumes the leading '(' has already been consumed.
121///
122/// parenexpr ::= expr)
123///
Daniel Dunbar475839e2009-06-29 20:37:27 +0000124bool AsmParser::ParseParenExpr(AsmExpr *&Res) {
Chris Lattner74ec1a32009-06-22 06:32:03 +0000125 if (ParseExpression(Res)) return true;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000126 if (Lexer.isNot(AsmToken::RParen))
Chris Lattner74ec1a32009-06-22 06:32:03 +0000127 return TokError("expected ')' in parentheses expression");
128 Lexer.Lex();
129 return false;
130}
Chris Lattnerc4193832009-06-22 05:51:26 +0000131
Chris Lattner74ec1a32009-06-22 06:32:03 +0000132/// ParsePrimaryExpr - Parse a primary expression and return it.
133/// primaryexpr ::= (parenexpr
134/// primaryexpr ::= symbol
135/// primaryexpr ::= number
136/// primaryexpr ::= ~,+,- primaryexpr
Daniel Dunbar475839e2009-06-29 20:37:27 +0000137bool AsmParser::ParsePrimaryExpr(AsmExpr *&Res) {
Chris Lattnerc4193832009-06-22 05:51:26 +0000138 switch (Lexer.getKind()) {
139 default:
140 return TokError("unknown token in expression");
Daniel Dunbar3f872332009-07-28 16:08:33 +0000141 case AsmToken::Exclaim:
Daniel Dunbar475839e2009-06-29 20:37:27 +0000142 Lexer.Lex(); // Eat the operator.
143 if (ParsePrimaryExpr(Res))
144 return true;
145 Res = new AsmUnaryExpr(AsmUnaryExpr::LNot, Res);
146 return false;
Daniel Dunbar76c4d762009-07-31 21:55:09 +0000147 case AsmToken::String:
Daniel Dunbar3f872332009-07-28 16:08:33 +0000148 case AsmToken::Identifier: {
Chris Lattnerc4193832009-06-22 05:51:26 +0000149 // This is a label, this should be parsed as part of an expression, to
Daniel Dunbar475839e2009-06-29 20:37:27 +0000150 // handle things like LFOO+4.
Daniel Dunbar76c4d762009-07-31 21:55:09 +0000151 MCSymbol *Sym = Ctx.GetOrCreateSymbol(Lexer.getTok().getIdentifier());
Daniel Dunbardce0f3c2009-06-29 23:43:14 +0000152
153 // If this is use of an undefined symbol then mark it external.
154 if (!Sym->getSection() && !Ctx.GetSymbolValue(Sym))
155 Sym->setExternal(true);
156
157 Res = new AsmSymbolRefExpr(Sym);
Chris Lattnerc4193832009-06-22 05:51:26 +0000158 Lexer.Lex(); // Eat identifier.
159 return false;
Daniel Dunbardce0f3c2009-06-29 23:43:14 +0000160 }
Daniel Dunbar3f872332009-07-28 16:08:33 +0000161 case AsmToken::Integer:
Daniel Dunbar419aded2009-07-28 16:38:40 +0000162 Res = new AsmConstantExpr(Lexer.getTok().getIntVal());
Daniel Dunbar76c4d762009-07-31 21:55:09 +0000163 Lexer.Lex(); // Eat token.
Chris Lattnerc4193832009-06-22 05:51:26 +0000164 return false;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000165 case AsmToken::LParen:
Chris Lattner74ec1a32009-06-22 06:32:03 +0000166 Lexer.Lex(); // Eat the '('.
167 return ParseParenExpr(Res);
Daniel Dunbar3f872332009-07-28 16:08:33 +0000168 case AsmToken::Minus:
Chris Lattner74ec1a32009-06-22 06:32:03 +0000169 Lexer.Lex(); // Eat the operator.
Daniel Dunbar475839e2009-06-29 20:37:27 +0000170 if (ParsePrimaryExpr(Res))
171 return true;
172 Res = new AsmUnaryExpr(AsmUnaryExpr::Minus, Res);
173 return false;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000174 case AsmToken::Plus:
Daniel Dunbar475839e2009-06-29 20:37:27 +0000175 Lexer.Lex(); // Eat the operator.
176 if (ParsePrimaryExpr(Res))
177 return true;
178 Res = new AsmUnaryExpr(AsmUnaryExpr::Plus, Res);
179 return false;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000180 case AsmToken::Tilde:
Daniel Dunbar475839e2009-06-29 20:37:27 +0000181 Lexer.Lex(); // Eat the operator.
182 if (ParsePrimaryExpr(Res))
183 return true;
184 Res = new AsmUnaryExpr(AsmUnaryExpr::Not, Res);
185 return false;
Chris Lattnerc4193832009-06-22 05:51:26 +0000186 }
187}
Chris Lattner74ec1a32009-06-22 06:32:03 +0000188
189/// ParseExpression - Parse an expression and return it.
190///
191/// expr ::= expr +,- expr -> lowest.
192/// expr ::= expr |,^,&,! expr -> middle.
193/// expr ::= expr *,/,%,<<,>> expr -> highest.
194/// expr ::= primaryexpr
195///
Daniel Dunbar475839e2009-06-29 20:37:27 +0000196bool AsmParser::ParseExpression(AsmExpr *&Res) {
197 Res = 0;
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000198 return ParsePrimaryExpr(Res) ||
199 ParseBinOpRHS(1, Res);
Chris Lattner74ec1a32009-06-22 06:32:03 +0000200}
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000201
Daniel Dunbar475839e2009-06-29 20:37:27 +0000202bool AsmParser::ParseAbsoluteExpression(int64_t &Res) {
203 AsmExpr *Expr;
204
Daniel Dunbarf4b830f2009-06-30 02:10:03 +0000205 SMLoc StartLoc = Lexer.getLoc();
Daniel Dunbar475839e2009-06-29 20:37:27 +0000206 if (ParseExpression(Expr))
207 return true;
208
209 if (!Expr->EvaluateAsAbsolute(Ctx, Res))
Daniel Dunbarf4b830f2009-06-30 02:10:03 +0000210 return Error(StartLoc, "expected absolute expression");
Daniel Dunbar475839e2009-06-29 20:37:27 +0000211
212 return false;
213}
214
Daniel Dunbar15d17072009-06-30 01:49:52 +0000215bool AsmParser::ParseRelocatableExpression(MCValue &Res) {
216 AsmExpr *Expr;
217
Daniel Dunbarf4b830f2009-06-30 02:10:03 +0000218 SMLoc StartLoc = Lexer.getLoc();
Daniel Dunbar15d17072009-06-30 01:49:52 +0000219 if (ParseExpression(Expr))
220 return true;
221
222 if (!Expr->EvaluateAsRelocatable(Ctx, Res))
Daniel Dunbarf4b830f2009-06-30 02:10:03 +0000223 return Error(StartLoc, "expected relocatable expression");
Daniel Dunbar15d17072009-06-30 01:49:52 +0000224
225 return false;
226}
227
Daniel Dunbar2c3f00c2009-07-02 02:09:07 +0000228bool AsmParser::ParseParenRelocatableExpression(MCValue &Res) {
229 AsmExpr *Expr;
230
231 SMLoc StartLoc = Lexer.getLoc();
232 if (ParseParenExpr(Expr))
233 return true;
234
235 if (!Expr->EvaluateAsRelocatable(Ctx, Res))
236 return Error(StartLoc, "expected relocatable expression");
237
238 return false;
239}
240
Daniel Dunbar3f872332009-07-28 16:08:33 +0000241static unsigned getBinOpPrecedence(AsmToken::TokenKind K,
Daniel Dunbar475839e2009-06-29 20:37:27 +0000242 AsmBinaryExpr::Opcode &Kind) {
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000243 switch (K) {
244 default: return 0; // not a binop.
Daniel Dunbar475839e2009-06-29 20:37:27 +0000245
246 // Lowest Precedence: &&, ||
Daniel Dunbar3f872332009-07-28 16:08:33 +0000247 case AsmToken::AmpAmp:
Daniel Dunbar475839e2009-06-29 20:37:27 +0000248 Kind = AsmBinaryExpr::LAnd;
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000249 return 1;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000250 case AsmToken::PipePipe:
Daniel Dunbar475839e2009-06-29 20:37:27 +0000251 Kind = AsmBinaryExpr::LOr;
252 return 1;
253
254 // Low Precedence: +, -, ==, !=, <>, <, <=, >, >=
Daniel Dunbar3f872332009-07-28 16:08:33 +0000255 case AsmToken::Plus:
Daniel Dunbar475839e2009-06-29 20:37:27 +0000256 Kind = AsmBinaryExpr::Add;
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000257 return 2;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000258 case AsmToken::Minus:
Daniel Dunbar475839e2009-06-29 20:37:27 +0000259 Kind = AsmBinaryExpr::Sub;
260 return 2;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000261 case AsmToken::EqualEqual:
Daniel Dunbar475839e2009-06-29 20:37:27 +0000262 Kind = AsmBinaryExpr::EQ;
263 return 2;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000264 case AsmToken::ExclaimEqual:
265 case AsmToken::LessGreater:
Daniel Dunbar475839e2009-06-29 20:37:27 +0000266 Kind = AsmBinaryExpr::NE;
267 return 2;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000268 case AsmToken::Less:
Daniel Dunbar475839e2009-06-29 20:37:27 +0000269 Kind = AsmBinaryExpr::LT;
270 return 2;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000271 case AsmToken::LessEqual:
Daniel Dunbar475839e2009-06-29 20:37:27 +0000272 Kind = AsmBinaryExpr::LTE;
273 return 2;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000274 case AsmToken::Greater:
Daniel Dunbar475839e2009-06-29 20:37:27 +0000275 Kind = AsmBinaryExpr::GT;
276 return 2;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000277 case AsmToken::GreaterEqual:
Daniel Dunbar475839e2009-06-29 20:37:27 +0000278 Kind = AsmBinaryExpr::GTE;
279 return 2;
280
281 // Intermediate Precedence: |, &, ^
282 //
283 // FIXME: gas seems to support '!' as an infix operator?
Daniel Dunbar3f872332009-07-28 16:08:33 +0000284 case AsmToken::Pipe:
Daniel Dunbar475839e2009-06-29 20:37:27 +0000285 Kind = AsmBinaryExpr::Or;
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000286 return 3;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000287 case AsmToken::Caret:
Daniel Dunbar475839e2009-06-29 20:37:27 +0000288 Kind = AsmBinaryExpr::Xor;
289 return 3;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000290 case AsmToken::Amp:
Daniel Dunbar475839e2009-06-29 20:37:27 +0000291 Kind = AsmBinaryExpr::And;
292 return 3;
293
294 // Highest Precedence: *, /, %, <<, >>
Daniel Dunbar3f872332009-07-28 16:08:33 +0000295 case AsmToken::Star:
Daniel Dunbar475839e2009-06-29 20:37:27 +0000296 Kind = AsmBinaryExpr::Mul;
297 return 4;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000298 case AsmToken::Slash:
Daniel Dunbar475839e2009-06-29 20:37:27 +0000299 Kind = AsmBinaryExpr::Div;
300 return 4;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000301 case AsmToken::Percent:
Daniel Dunbar475839e2009-06-29 20:37:27 +0000302 Kind = AsmBinaryExpr::Mod;
303 return 4;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000304 case AsmToken::LessLess:
Daniel Dunbar475839e2009-06-29 20:37:27 +0000305 Kind = AsmBinaryExpr::Shl;
306 return 4;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000307 case AsmToken::GreaterGreater:
Daniel Dunbar475839e2009-06-29 20:37:27 +0000308 Kind = AsmBinaryExpr::Shr;
309 return 4;
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000310 }
311}
312
313
314/// ParseBinOpRHS - Parse all binary operators with precedence >= 'Precedence'.
315/// Res contains the LHS of the expression on input.
Daniel Dunbar475839e2009-06-29 20:37:27 +0000316bool AsmParser::ParseBinOpRHS(unsigned Precedence, AsmExpr *&Res) {
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000317 while (1) {
Daniel Dunbar51330632009-06-29 21:14:21 +0000318 AsmBinaryExpr::Opcode Kind = AsmBinaryExpr::Add;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000319 unsigned TokPrec = getBinOpPrecedence(Lexer.getKind(), Kind);
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000320
321 // If the next token is lower precedence than we are allowed to eat, return
322 // successfully with what we ate already.
323 if (TokPrec < Precedence)
324 return false;
325
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000326 Lexer.Lex();
327
328 // Eat the next primary expression.
Daniel Dunbar475839e2009-06-29 20:37:27 +0000329 AsmExpr *RHS;
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000330 if (ParsePrimaryExpr(RHS)) return true;
331
332 // If BinOp binds less tightly with RHS than the operator after RHS, let
333 // the pending operator take RHS as its LHS.
Daniel Dunbar475839e2009-06-29 20:37:27 +0000334 AsmBinaryExpr::Opcode Dummy;
335 unsigned NextTokPrec = getBinOpPrecedence(Lexer.getKind(), Dummy);
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000336 if (TokPrec < NextTokPrec) {
337 if (ParseBinOpRHS(Precedence+1, RHS)) return true;
338 }
339
Daniel Dunbar475839e2009-06-29 20:37:27 +0000340 // Merge LHS and RHS according to operator.
341 Res = new AsmBinaryExpr(Kind, Res, RHS);
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000342 }
343}
344
Chris Lattnerc4193832009-06-22 05:51:26 +0000345
346
347
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000348/// ParseStatement:
349/// ::= EndOfStatement
Chris Lattner2cf5f142009-06-22 01:29:09 +0000350/// ::= Label* Directive ...Operands... EndOfStatement
351/// ::= Label* Identifier OperandList* EndOfStatement
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000352bool AsmParser::ParseStatement() {
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +0000353 if (Lexer.is(AsmToken::EndOfStatement)) {
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000354 Lexer.Lex();
355 return false;
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000356 }
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +0000357
358 // Statements always start with an identifier.
Daniel Dunbar419aded2009-07-28 16:38:40 +0000359 AsmToken ID = Lexer.getTok();
360 SMLoc IDLoc = ID.getLoc();
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +0000361 StringRef IDVal;
362 if (ParseIdentifier(IDVal))
363 return TokError("unexpected token at start of statement");
364
365 // FIXME: Recurse on local labels?
366
367 // See what kind of statement we have.
368 switch (Lexer.getKind()) {
Daniel Dunbar3f872332009-07-28 16:08:33 +0000369 case AsmToken::Colon: {
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000370 // identifier ':' -> Label.
371 Lexer.Lex();
Daniel Dunbardce0f3c2009-06-29 23:43:14 +0000372
373 // Diagnose attempt to use a variable as a label.
374 //
375 // FIXME: Diagnostics. Note the location of the definition as a label.
376 // FIXME: This doesn't diagnose assignment to a symbol which has been
377 // implicitly marked as external.
378 MCSymbol *Sym = Ctx.GetOrCreateSymbol(IDVal);
379 if (Sym->getSection())
380 return Error(IDLoc, "invalid symbol redefinition");
381 if (Ctx.GetSymbolValue(Sym))
382 return Error(IDLoc, "symbol already used as assembler variable");
Chris Lattnerc69485e2009-06-24 04:31:49 +0000383
384 // Since we saw a label, create a symbol and emit it.
385 // FIXME: If the label starts with L it is an assembler temporary label.
386 // Why does the client of this api need to know this?
Daniel Dunbardce0f3c2009-06-29 23:43:14 +0000387 Out.EmitLabel(Sym);
388
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000389 return ParseStatement();
Daniel Dunbardce0f3c2009-06-29 23:43:14 +0000390 }
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000391
Daniel Dunbar3f872332009-07-28 16:08:33 +0000392 case AsmToken::Equal:
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000393 // identifier '=' ... -> assignment statement
394 Lexer.Lex();
395
396 return ParseAssignment(IDVal, false);
397
398 default: // Normal instruction or directive.
399 break;
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000400 }
401
402 // Otherwise, we have a normal instruction or directive.
Chris Lattner2cf5f142009-06-22 01:29:09 +0000403 if (IDVal[0] == '.') {
Chris Lattner529fb542009-06-24 05:13:15 +0000404 // FIXME: This should be driven based on a hash lookup and callback.
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000405 if (IDVal == ".section")
Chris Lattner529fb542009-06-24 05:13:15 +0000406 return ParseDirectiveDarwinSection();
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000407 if (IDVal == ".text")
Chris Lattner529fb542009-06-24 05:13:15 +0000408 // FIXME: This changes behavior based on the -static flag to the
409 // assembler.
Chris Lattnerff4bc462009-08-10 01:39:42 +0000410 return ParseDirectiveSectionSwitch("__TEXT", "__text",
411 MCSectionMachO::S_ATTR_PURE_INSTRUCTIONS);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000412 if (IDVal == ".const")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000413 return ParseDirectiveSectionSwitch("__TEXT", "__const");
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000414 if (IDVal == ".static_const")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000415 return ParseDirectiveSectionSwitch("__TEXT", "__static_const");
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000416 if (IDVal == ".cstring")
Chris Lattnerff4bc462009-08-10 01:39:42 +0000417 return ParseDirectiveSectionSwitch("__TEXT","__cstring",
418 MCSectionMachO::S_CSTRING_LITERALS);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000419 if (IDVal == ".literal4")
Chris Lattnerff4bc462009-08-10 01:39:42 +0000420 return ParseDirectiveSectionSwitch("__TEXT", "__literal4",
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000421 MCSectionMachO::S_4BYTE_LITERALS,
422 4);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000423 if (IDVal == ".literal8")
Chris Lattnerff4bc462009-08-10 01:39:42 +0000424 return ParseDirectiveSectionSwitch("__TEXT", "__literal8",
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000425 MCSectionMachO::S_8BYTE_LITERALS,
426 8);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000427 if (IDVal == ".literal16")
Chris Lattnerff4bc462009-08-10 01:39:42 +0000428 return ParseDirectiveSectionSwitch("__TEXT","__literal16",
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000429 MCSectionMachO::S_16BYTE_LITERALS,
430 16);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000431 if (IDVal == ".constructor")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000432 return ParseDirectiveSectionSwitch("__TEXT","__constructor");
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000433 if (IDVal == ".destructor")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000434 return ParseDirectiveSectionSwitch("__TEXT","__destructor");
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000435 if (IDVal == ".fvmlib_init0")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000436 return ParseDirectiveSectionSwitch("__TEXT","__fvmlib_init0");
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000437 if (IDVal == ".fvmlib_init1")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000438 return ParseDirectiveSectionSwitch("__TEXT","__fvmlib_init1");
439
440 // FIXME: The assembler manual claims that this has the self modify code
441 // flag, at least on x86-32, but that does not appear to be correct.
Chris Lattnerff4bc462009-08-10 01:39:42 +0000442 if (IDVal == ".symbol_stub")
443 return ParseDirectiveSectionSwitch("__TEXT","__symbol_stub",
444 MCSectionMachO::S_SYMBOL_STUBS |
Chris Lattnerff4bc462009-08-10 01:39:42 +0000445 MCSectionMachO::S_ATTR_PURE_INSTRUCTIONS,
446 // FIXME: Different on PPC and ARM.
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000447 0, 16);
448 // FIXME: PowerPC only?
449 if (IDVal == ".picsymbol_stub")
450 return ParseDirectiveSectionSwitch("__TEXT","__picsymbol_stub",
451 MCSectionMachO::S_SYMBOL_STUBS |
452 MCSectionMachO::S_ATTR_PURE_INSTRUCTIONS,
453 0, 26);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000454 if (IDVal == ".data")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000455 return ParseDirectiveSectionSwitch("__DATA", "__data");
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000456 if (IDVal == ".static_data")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000457 return ParseDirectiveSectionSwitch("__DATA", "__static_data");
458
459 // FIXME: The section names of these two are misspelled in the assembler
460 // manual.
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000461 if (IDVal == ".non_lazy_symbol_pointer")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000462 return ParseDirectiveSectionSwitch("__DATA", "__nl_symbol_ptr",
463 MCSectionMachO::S_NON_LAZY_SYMBOL_POINTERS,
464 4);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000465 if (IDVal == ".lazy_symbol_pointer")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000466 return ParseDirectiveSectionSwitch("__DATA", "__la_symbol_ptr",
467 MCSectionMachO::S_LAZY_SYMBOL_POINTERS,
468 4);
469
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000470 if (IDVal == ".dyld")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000471 return ParseDirectiveSectionSwitch("__DATA", "__dyld");
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000472 if (IDVal == ".mod_init_func")
Chris Lattnerff4bc462009-08-10 01:39:42 +0000473 return ParseDirectiveSectionSwitch("__DATA", "__mod_init_func",
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000474 MCSectionMachO::S_MOD_INIT_FUNC_POINTERS,
475 4);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000476 if (IDVal == ".mod_term_func")
Chris Lattnerff4bc462009-08-10 01:39:42 +0000477 return ParseDirectiveSectionSwitch("__DATA", "__mod_term_func",
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000478 MCSectionMachO::S_MOD_TERM_FUNC_POINTERS,
479 4);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000480 if (IDVal == ".const_data")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000481 return ParseDirectiveSectionSwitch("__DATA", "__const");
Chris Lattner529fb542009-06-24 05:13:15 +0000482
483
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000484 if (IDVal == ".objc_class")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000485 return ParseDirectiveSectionSwitch("__OBJC", "__class",
486 MCSectionMachO::S_ATTR_NO_DEAD_STRIP);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000487 if (IDVal == ".objc_meta_class")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000488 return ParseDirectiveSectionSwitch("__OBJC", "__meta_class",
489 MCSectionMachO::S_ATTR_NO_DEAD_STRIP);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000490 if (IDVal == ".objc_cat_cls_meth")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000491 return ParseDirectiveSectionSwitch("__OBJC", "__cat_cls_meth",
492 MCSectionMachO::S_ATTR_NO_DEAD_STRIP);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000493 if (IDVal == ".objc_cat_inst_meth")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000494 return ParseDirectiveSectionSwitch("__OBJC", "__cat_inst_meth",
495 MCSectionMachO::S_ATTR_NO_DEAD_STRIP);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000496 if (IDVal == ".objc_protocol")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000497 return ParseDirectiveSectionSwitch("__OBJC", "__protocol",
498 MCSectionMachO::S_ATTR_NO_DEAD_STRIP);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000499 if (IDVal == ".objc_string_object")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000500 return ParseDirectiveSectionSwitch("__OBJC", "__string_object",
501 MCSectionMachO::S_ATTR_NO_DEAD_STRIP);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000502 if (IDVal == ".objc_cls_meth")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000503 return ParseDirectiveSectionSwitch("__OBJC", "__cls_meth",
504 MCSectionMachO::S_ATTR_NO_DEAD_STRIP);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000505 if (IDVal == ".objc_inst_meth")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000506 return ParseDirectiveSectionSwitch("__OBJC", "__inst_meth",
507 MCSectionMachO::S_ATTR_NO_DEAD_STRIP);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000508 if (IDVal == ".objc_cls_refs")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000509 return ParseDirectiveSectionSwitch("__OBJC", "__cls_refs",
510 MCSectionMachO::S_ATTR_NO_DEAD_STRIP |
511 MCSectionMachO::S_LITERAL_POINTERS,
512 4);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000513 if (IDVal == ".objc_message_refs")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000514 return ParseDirectiveSectionSwitch("__OBJC", "__message_refs",
515 MCSectionMachO::S_ATTR_NO_DEAD_STRIP |
516 MCSectionMachO::S_LITERAL_POINTERS,
517 4);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000518 if (IDVal == ".objc_symbols")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000519 return ParseDirectiveSectionSwitch("__OBJC", "__symbols",
520 MCSectionMachO::S_ATTR_NO_DEAD_STRIP);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000521 if (IDVal == ".objc_category")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000522 return ParseDirectiveSectionSwitch("__OBJC", "__category",
523 MCSectionMachO::S_ATTR_NO_DEAD_STRIP);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000524 if (IDVal == ".objc_class_vars")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000525 return ParseDirectiveSectionSwitch("__OBJC", "__class_vars",
526 MCSectionMachO::S_ATTR_NO_DEAD_STRIP);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000527 if (IDVal == ".objc_instance_vars")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000528 return ParseDirectiveSectionSwitch("__OBJC", "__instance_vars",
529 MCSectionMachO::S_ATTR_NO_DEAD_STRIP);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000530 if (IDVal == ".objc_module_info")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000531 return ParseDirectiveSectionSwitch("__OBJC", "__module_info",
532 MCSectionMachO::S_ATTR_NO_DEAD_STRIP);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000533 if (IDVal == ".objc_class_names")
Chris Lattnerff4bc462009-08-10 01:39:42 +0000534 return ParseDirectiveSectionSwitch("__TEXT", "__cstring",
535 MCSectionMachO::S_CSTRING_LITERALS);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000536 if (IDVal == ".objc_meth_var_types")
Chris Lattnerff4bc462009-08-10 01:39:42 +0000537 return ParseDirectiveSectionSwitch("__TEXT", "__cstring",
538 MCSectionMachO::S_CSTRING_LITERALS);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000539 if (IDVal == ".objc_meth_var_names")
Chris Lattnerff4bc462009-08-10 01:39:42 +0000540 return ParseDirectiveSectionSwitch("__TEXT", "__cstring",
541 MCSectionMachO::S_CSTRING_LITERALS);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000542 if (IDVal == ".objc_selector_strs")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000543 return ParseDirectiveSectionSwitch("__OBJC", "__selector_strs",
544 MCSectionMachO::S_CSTRING_LITERALS);
Chris Lattner9a023f72009-06-24 04:43:34 +0000545
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000546 // Assembler features
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000547 if (IDVal == ".set")
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000548 return ParseDirectiveSet();
549
Daniel Dunbara0d14262009-06-24 23:30:00 +0000550 // Data directives
551
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000552 if (IDVal == ".ascii")
Daniel Dunbara0d14262009-06-24 23:30:00 +0000553 return ParseDirectiveAscii(false);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000554 if (IDVal == ".asciz")
Daniel Dunbara0d14262009-06-24 23:30:00 +0000555 return ParseDirectiveAscii(true);
556
557 // FIXME: Target hooks for size? Also for "word", "hword".
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000558 if (IDVal == ".byte")
Daniel Dunbara0d14262009-06-24 23:30:00 +0000559 return ParseDirectiveValue(1);
Daniel Dunbar1e840b22009-08-11 04:44:00 +0000560 if (IDVal == ".short" || IDVal == ".word")
Daniel Dunbara0d14262009-06-24 23:30:00 +0000561 return ParseDirectiveValue(2);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000562 if (IDVal == ".long")
Daniel Dunbara0d14262009-06-24 23:30:00 +0000563 return ParseDirectiveValue(4);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000564 if (IDVal == ".quad")
Daniel Dunbara0d14262009-06-24 23:30:00 +0000565 return ParseDirectiveValue(8);
Daniel Dunbarc29dfa72009-06-29 23:46:59 +0000566
567 // FIXME: Target hooks for IsPow2.
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000568 if (IDVal == ".align")
Daniel Dunbarc29dfa72009-06-29 23:46:59 +0000569 return ParseDirectiveAlign(/*IsPow2=*/true, /*ExprSize=*/1);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000570 if (IDVal == ".align32")
Daniel Dunbarc29dfa72009-06-29 23:46:59 +0000571 return ParseDirectiveAlign(/*IsPow2=*/true, /*ExprSize=*/4);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000572 if (IDVal == ".balign")
Daniel Dunbarc29dfa72009-06-29 23:46:59 +0000573 return ParseDirectiveAlign(/*IsPow2=*/false, /*ExprSize=*/1);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000574 if (IDVal == ".balignw")
Daniel Dunbarc29dfa72009-06-29 23:46:59 +0000575 return ParseDirectiveAlign(/*IsPow2=*/false, /*ExprSize=*/2);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000576 if (IDVal == ".balignl")
Daniel Dunbarc29dfa72009-06-29 23:46:59 +0000577 return ParseDirectiveAlign(/*IsPow2=*/false, /*ExprSize=*/4);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000578 if (IDVal == ".p2align")
Daniel Dunbarc29dfa72009-06-29 23:46:59 +0000579 return ParseDirectiveAlign(/*IsPow2=*/true, /*ExprSize=*/1);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000580 if (IDVal == ".p2alignw")
Daniel Dunbarc29dfa72009-06-29 23:46:59 +0000581 return ParseDirectiveAlign(/*IsPow2=*/true, /*ExprSize=*/2);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000582 if (IDVal == ".p2alignl")
Daniel Dunbarc29dfa72009-06-29 23:46:59 +0000583 return ParseDirectiveAlign(/*IsPow2=*/true, /*ExprSize=*/4);
584
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000585 if (IDVal == ".org")
Daniel Dunbarc238b582009-06-25 22:44:51 +0000586 return ParseDirectiveOrg();
Daniel Dunbarc29dfa72009-06-29 23:46:59 +0000587
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000588 if (IDVal == ".fill")
Daniel Dunbarc29dfa72009-06-29 23:46:59 +0000589 return ParseDirectiveFill();
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000590 if (IDVal == ".space")
Daniel Dunbara0d14262009-06-24 23:30:00 +0000591 return ParseDirectiveSpace();
592
Daniel Dunbard7b267b2009-06-30 00:33:19 +0000593 // Symbol attribute directives
Daniel Dunbard0c14d62009-08-11 04:24:50 +0000594
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000595 if (IDVal == ".globl" || IDVal == ".global")
Daniel Dunbard7b267b2009-06-30 00:33:19 +0000596 return ParseDirectiveSymbolAttribute(MCStreamer::Global);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000597 if (IDVal == ".hidden")
Daniel Dunbard7b267b2009-06-30 00:33:19 +0000598 return ParseDirectiveSymbolAttribute(MCStreamer::Hidden);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000599 if (IDVal == ".indirect_symbol")
Daniel Dunbard7b267b2009-06-30 00:33:19 +0000600 return ParseDirectiveSymbolAttribute(MCStreamer::IndirectSymbol);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000601 if (IDVal == ".internal")
Daniel Dunbard7b267b2009-06-30 00:33:19 +0000602 return ParseDirectiveSymbolAttribute(MCStreamer::Internal);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000603 if (IDVal == ".lazy_reference")
Daniel Dunbard7b267b2009-06-30 00:33:19 +0000604 return ParseDirectiveSymbolAttribute(MCStreamer::LazyReference);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000605 if (IDVal == ".no_dead_strip")
Daniel Dunbard7b267b2009-06-30 00:33:19 +0000606 return ParseDirectiveSymbolAttribute(MCStreamer::NoDeadStrip);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000607 if (IDVal == ".private_extern")
Daniel Dunbard7b267b2009-06-30 00:33:19 +0000608 return ParseDirectiveSymbolAttribute(MCStreamer::PrivateExtern);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000609 if (IDVal == ".protected")
Daniel Dunbard7b267b2009-06-30 00:33:19 +0000610 return ParseDirectiveSymbolAttribute(MCStreamer::Protected);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000611 if (IDVal == ".reference")
Daniel Dunbard7b267b2009-06-30 00:33:19 +0000612 return ParseDirectiveSymbolAttribute(MCStreamer::Reference);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000613 if (IDVal == ".weak")
Daniel Dunbard7b267b2009-06-30 00:33:19 +0000614 return ParseDirectiveSymbolAttribute(MCStreamer::Weak);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000615 if (IDVal == ".weak_definition")
Daniel Dunbard7b267b2009-06-30 00:33:19 +0000616 return ParseDirectiveSymbolAttribute(MCStreamer::WeakDefinition);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000617 if (IDVal == ".weak_reference")
Daniel Dunbard7b267b2009-06-30 00:33:19 +0000618 return ParseDirectiveSymbolAttribute(MCStreamer::WeakReference);
619
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000620 if (IDVal == ".comm")
Chris Lattner1fc3d752009-07-09 17:25:12 +0000621 return ParseDirectiveComm(/*IsLocal=*/false);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000622 if (IDVal == ".lcomm")
Chris Lattner1fc3d752009-07-09 17:25:12 +0000623 return ParseDirectiveComm(/*IsLocal=*/true);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000624 if (IDVal == ".zerofill")
Chris Lattner9be3fee2009-07-10 22:20:30 +0000625 return ParseDirectiveDarwinZerofill();
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000626 if (IDVal == ".desc")
Kevin Enderby95cf30c2009-07-14 18:17:10 +0000627 return ParseDirectiveDarwinSymbolDesc();
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000628 if (IDVal == ".lsym")
Kevin Enderby71148242009-07-14 21:35:03 +0000629 return ParseDirectiveDarwinLsym();
Chris Lattner4e4db7a2009-07-07 20:30:46 +0000630
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000631 if (IDVal == ".subsections_via_symbols")
Kevin Enderbya5c78322009-07-13 21:03:15 +0000632 return ParseDirectiveDarwinSubsectionsViaSymbols();
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000633 if (IDVal == ".abort")
Kevin Enderby5f1f0b82009-07-13 23:15:14 +0000634 return ParseDirectiveAbort();
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000635 if (IDVal == ".include")
Kevin Enderby1f049b22009-07-14 23:21:55 +0000636 return ParseDirectiveInclude();
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000637 if (IDVal == ".dump")
Kevin Enderby5026ae42009-07-20 20:25:37 +0000638 return ParseDirectiveDarwinDumpOrLoad(IDLoc, /*IsDump=*/true);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000639 if (IDVal == ".load")
Kevin Enderby5026ae42009-07-20 20:25:37 +0000640 return ParseDirectiveDarwinDumpOrLoad(IDLoc, /*IsLoad=*/false);
Kevin Enderbya5c78322009-07-13 21:03:15 +0000641
Daniel Dunbard0c14d62009-08-11 04:24:50 +0000642 // Debugging directives
643
644 if (IDVal == ".file")
645 return ParseDirectiveFile(IDLoc);
646 if (IDVal == ".line")
647 return ParseDirectiveLine(IDLoc);
648 if (IDVal == ".loc")
649 return ParseDirectiveLoc(IDLoc);
650
Daniel Dunbar3fb76832009-06-30 00:49:23 +0000651 Warning(IDLoc, "ignoring directive for now");
Chris Lattner2cf5f142009-06-22 01:29:09 +0000652 EatToEndOfStatement();
653 return false;
654 }
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000655
Chris Lattner29dfe7c2009-06-23 18:41:30 +0000656 MCInst Inst;
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000657 if (getTargetParser().ParseInstruction(IDVal, Inst))
Chris Lattner29dfe7c2009-06-23 18:41:30 +0000658 return true;
Chris Lattner2cf5f142009-06-22 01:29:09 +0000659
Daniel Dunbar3f872332009-07-28 16:08:33 +0000660 if (Lexer.isNot(AsmToken::EndOfStatement))
Chris Lattner9a023f72009-06-24 04:43:34 +0000661 return TokError("unexpected token in argument list");
Chris Lattner2cf5f142009-06-22 01:29:09 +0000662
663 // Eat the end of statement marker.
664 Lexer.Lex();
665
666 // Instruction is good, process it.
Daniel Dunbar0eebb052009-07-01 06:35:48 +0000667 Out.EmitInstruction(Inst);
Chris Lattner2cf5f142009-06-22 01:29:09 +0000668
669 // Skip to end of line for now.
Chris Lattner27aa7d22009-06-21 20:16:42 +0000670 return false;
671}
Chris Lattner9a023f72009-06-24 04:43:34 +0000672
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000673bool AsmParser::ParseAssignment(const StringRef &Name, bool IsDotSet) {
Daniel Dunbardce0f3c2009-06-29 23:43:14 +0000674 // FIXME: Use better location, we should use proper tokens.
675 SMLoc EqualLoc = Lexer.getLoc();
676
Daniel Dunbarf4b830f2009-06-30 02:10:03 +0000677 MCValue Value;
678 if (ParseRelocatableExpression(Value))
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000679 return true;
680
Daniel Dunbar3f872332009-07-28 16:08:33 +0000681 if (Lexer.isNot(AsmToken::EndOfStatement))
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000682 return TokError("unexpected token in assignment");
683
684 // Eat the end of statement marker.
685 Lexer.Lex();
686
Daniel Dunbardce0f3c2009-06-29 23:43:14 +0000687 // Diagnose assignment to a label.
688 //
689 // FIXME: Diagnostics. Note the location of the definition as a label.
690 // FIXME: This doesn't diagnose assignment to a symbol which has been
691 // implicitly marked as external.
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000692 // FIXME: Handle '.'.
Daniel Dunbardce0f3c2009-06-29 23:43:14 +0000693 // FIXME: Diagnose assignment to protected identifier (e.g., register name).
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000694 MCSymbol *Sym = Ctx.GetOrCreateSymbol(Name);
Daniel Dunbardce0f3c2009-06-29 23:43:14 +0000695 if (Sym->getSection())
696 return Error(EqualLoc, "invalid assignment to symbol emitted as a label");
697 if (Sym->isExternal())
698 return Error(EqualLoc, "invalid assignment to external symbol");
699
700 // Do the assignment.
Daniel Dunbarf4b830f2009-06-30 02:10:03 +0000701 Out.EmitAssignment(Sym, Value, IsDotSet);
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000702
703 return false;
704}
705
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +0000706/// ParseIdentifier:
707/// ::= identifier
708/// ::= string
709bool AsmParser::ParseIdentifier(StringRef &Res) {
710 if (Lexer.isNot(AsmToken::Identifier) &&
711 Lexer.isNot(AsmToken::String))
712 return true;
713
714 Res = Lexer.getTok().getIdentifier();
715
716 Lexer.Lex(); // Consume the identifier token.
717
718 return false;
719}
720
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000721/// ParseDirectiveSet:
722/// ::= .set identifier ',' expression
723bool AsmParser::ParseDirectiveSet() {
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +0000724 StringRef Name;
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000725
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +0000726 if (ParseIdentifier(Name))
727 return TokError("expected identifier after '.set' directive");
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000728
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +0000729 if (Lexer.isNot(AsmToken::Comma))
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000730 return TokError("unexpected token in '.set'");
731 Lexer.Lex();
732
733 return ParseAssignment(Name, true);
734}
735
Chris Lattner9a023f72009-06-24 04:43:34 +0000736/// ParseDirectiveSection:
Chris Lattner529fb542009-06-24 05:13:15 +0000737/// ::= .section identifier (',' identifier)*
738/// FIXME: This should actually parse out the segment, section, attributes and
739/// sizeof_stub fields.
740bool AsmParser::ParseDirectiveDarwinSection() {
Daniel Dunbarace63122009-08-11 03:42:33 +0000741 SMLoc Loc = Lexer.getLoc();
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +0000742
Daniel Dunbarace63122009-08-11 03:42:33 +0000743 StringRef SectionName;
744 if (ParseIdentifier(SectionName))
745 return Error(Loc, "expected identifier after '.section' directive");
746
747 // Verify there is a following comma.
748 if (!Lexer.is(AsmToken::Comma))
749 return TokError("unexpected token in '.section' directive");
750
Chris Lattnerff4bc462009-08-10 01:39:42 +0000751 std::string SectionSpec = SectionName;
Daniel Dunbarace63122009-08-11 03:42:33 +0000752 SectionSpec += ",";
753
754 // Add all the tokens until the end of the line, ParseSectionSpecifier will
755 // handle this.
Chris Lattnerff4bc462009-08-10 01:39:42 +0000756 StringRef EOL = Lexer.LexUntilEndOfStatement();
757 SectionSpec.append(EOL.begin(), EOL.end());
Daniel Dunbarace63122009-08-11 03:42:33 +0000758
Chris Lattnerff4bc462009-08-10 01:39:42 +0000759 Lexer.Lex();
Daniel Dunbar3f872332009-07-28 16:08:33 +0000760 if (Lexer.isNot(AsmToken::EndOfStatement))
Chris Lattner9a023f72009-06-24 04:43:34 +0000761 return TokError("unexpected token in '.section' directive");
762 Lexer.Lex();
763
Chris Lattnerff4bc462009-08-10 01:39:42 +0000764
765 StringRef Segment, Section;
766 unsigned TAA, StubSize;
767 std::string ErrorStr =
768 MCSectionMachO::ParseSectionSpecifier(SectionSpec, Segment, Section,
769 TAA, StubSize);
770
771 if (!ErrorStr.empty())
Daniel Dunbarace63122009-08-11 03:42:33 +0000772 return Error(Loc, ErrorStr.c_str());
Chris Lattnerff4bc462009-08-10 01:39:42 +0000773
774 // FIXME: CACHE THESE.
775
Chris Lattner56594f92009-07-31 17:47:16 +0000776 // FIXME: Arch specific.
Chris Lattnerff4bc462009-08-10 01:39:42 +0000777 MCSection *S = 0; //Ctx.GetSection(Section);
Chris Lattner56594f92009-07-31 17:47:16 +0000778 if (S == 0)
Chris Lattnerff4bc462009-08-10 01:39:42 +0000779 S = MCSectionMachO::Create(Segment, Section, TAA, StubSize,
780 SectionKind(), Ctx);
Chris Lattner56594f92009-07-31 17:47:16 +0000781
782 Out.SwitchSection(S);
Chris Lattner9a023f72009-06-24 04:43:34 +0000783 return false;
784}
785
Chris Lattnere15c2d72009-08-10 18:05:55 +0000786/// ParseDirectiveSectionSwitch -
Chris Lattnerff4bc462009-08-10 01:39:42 +0000787bool AsmParser::ParseDirectiveSectionSwitch(const char *Segment,
788 const char *Section,
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000789 unsigned TAA, unsigned Align,
790 unsigned StubSize) {
Daniel Dunbar3f872332009-07-28 16:08:33 +0000791 if (Lexer.isNot(AsmToken::EndOfStatement))
Chris Lattner529fb542009-06-24 05:13:15 +0000792 return TokError("unexpected token in section switching directive");
793 Lexer.Lex();
794
Chris Lattner56594f92009-07-31 17:47:16 +0000795 // FIXME: Arch specific.
Chris Lattnerff4bc462009-08-10 01:39:42 +0000796 // FIXME: Cache this!
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000797 // FIXME: Handle the implicit alignment!!
Chris Lattnerff4bc462009-08-10 01:39:42 +0000798 MCSection *S = 0; // Ctx.GetSection(Section);
Chris Lattner56594f92009-07-31 17:47:16 +0000799 if (S == 0)
Chris Lattnerff4bc462009-08-10 01:39:42 +0000800 S = MCSectionMachO::Create(Segment, Section, TAA, StubSize,
801 SectionKind(), Ctx);
Chris Lattner56594f92009-07-31 17:47:16 +0000802
803 Out.SwitchSection(S);
Chris Lattner529fb542009-06-24 05:13:15 +0000804 return false;
805}
Daniel Dunbara0d14262009-06-24 23:30:00 +0000806
Daniel Dunbar1ab75942009-08-14 18:19:52 +0000807bool AsmParser::ParseEscapedString(std::string &Data) {
808 assert(Lexer.is(AsmToken::String) && "Unexpected current token!");
809
810 Data = "";
811 StringRef Str = Lexer.getTok().getStringContents();
812 for (unsigned i = 0, e = Str.size(); i != e; ++i) {
813 if (Str[i] != '\\') {
814 Data += Str[i];
815 continue;
816 }
817
818 // Recognize escaped characters. Note that this escape semantics currently
819 // loosely follows Darwin 'as'. Notably, it doesn't support hex escapes.
820 ++i;
821 if (i == e)
822 return TokError("unexpected backslash at end of string");
823
824 // Recognize octal sequences.
825 if ((unsigned) (Str[i] - '0') <= 7) {
826 // Consume up to three octal characters.
827 unsigned Value = Str[i] - '0';
828
829 if (i + 1 != e && ((unsigned) (Str[i + 1] - '0')) <= 7) {
830 ++i;
831 Value = Value * 8 + (Str[i] - '0');
832
833 if (i + 1 != e && ((unsigned) (Str[i + 1] - '0')) <= 7) {
834 ++i;
835 Value = Value * 8 + (Str[i] - '0');
836 }
837 }
838
839 if (Value > 255)
840 return TokError("invalid octal escape sequence (out of range)");
841
842 Data += (unsigned char) Value;
843 continue;
844 }
845
846 // Otherwise recognize individual escapes.
847 switch (Str[i]) {
848 default:
849 // Just reject invalid escape sequences for now.
850 return TokError("invalid escape sequence (unrecognized character)");
851
852 case 'b': Data += '\b'; break;
853 case 'f': Data += '\f'; break;
854 case 'n': Data += '\n'; break;
855 case 'r': Data += '\r'; break;
856 case 't': Data += '\t'; break;
857 case '"': Data += '"'; break;
858 case '\\': Data += '\\'; break;
859 }
860 }
861
862 return false;
863}
864
Daniel Dunbara0d14262009-06-24 23:30:00 +0000865/// ParseDirectiveAscii:
Daniel Dunbar475839e2009-06-29 20:37:27 +0000866/// ::= ( .ascii | .asciz ) [ "string" ( , "string" )* ]
Daniel Dunbara0d14262009-06-24 23:30:00 +0000867bool AsmParser::ParseDirectiveAscii(bool ZeroTerminated) {
Daniel Dunbar3f872332009-07-28 16:08:33 +0000868 if (Lexer.isNot(AsmToken::EndOfStatement)) {
Daniel Dunbara0d14262009-06-24 23:30:00 +0000869 for (;;) {
Daniel Dunbar3f872332009-07-28 16:08:33 +0000870 if (Lexer.isNot(AsmToken::String))
Daniel Dunbara0d14262009-06-24 23:30:00 +0000871 return TokError("expected string in '.ascii' or '.asciz' directive");
872
Daniel Dunbar1ab75942009-08-14 18:19:52 +0000873 std::string Data;
874 if (ParseEscapedString(Data))
875 return true;
876
877 Out.EmitBytes(Data);
Daniel Dunbara0d14262009-06-24 23:30:00 +0000878 if (ZeroTerminated)
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000879 Out.EmitBytes(StringRef("\0", 1));
Daniel Dunbara0d14262009-06-24 23:30:00 +0000880
881 Lexer.Lex();
882
Daniel Dunbar3f872332009-07-28 16:08:33 +0000883 if (Lexer.is(AsmToken::EndOfStatement))
Daniel Dunbara0d14262009-06-24 23:30:00 +0000884 break;
885
Daniel Dunbar3f872332009-07-28 16:08:33 +0000886 if (Lexer.isNot(AsmToken::Comma))
Daniel Dunbara0d14262009-06-24 23:30:00 +0000887 return TokError("unexpected token in '.ascii' or '.asciz' directive");
888 Lexer.Lex();
889 }
890 }
891
892 Lexer.Lex();
893 return false;
894}
895
896/// ParseDirectiveValue
897/// ::= (.byte | .short | ... ) [ expression (, expression)* ]
898bool AsmParser::ParseDirectiveValue(unsigned Size) {
Daniel Dunbar3f872332009-07-28 16:08:33 +0000899 if (Lexer.isNot(AsmToken::EndOfStatement)) {
Daniel Dunbara0d14262009-06-24 23:30:00 +0000900 for (;;) {
Daniel Dunbarf4b830f2009-06-30 02:10:03 +0000901 MCValue Expr;
902 if (ParseRelocatableExpression(Expr))
Daniel Dunbara0d14262009-06-24 23:30:00 +0000903 return true;
904
Daniel Dunbarf4b830f2009-06-30 02:10:03 +0000905 Out.EmitValue(Expr, Size);
Daniel Dunbara0d14262009-06-24 23:30:00 +0000906
Daniel Dunbar3f872332009-07-28 16:08:33 +0000907 if (Lexer.is(AsmToken::EndOfStatement))
Daniel Dunbara0d14262009-06-24 23:30:00 +0000908 break;
909
910 // FIXME: Improve diagnostic.
Daniel Dunbar3f872332009-07-28 16:08:33 +0000911 if (Lexer.isNot(AsmToken::Comma))
Daniel Dunbara0d14262009-06-24 23:30:00 +0000912 return TokError("unexpected token in directive");
913 Lexer.Lex();
914 }
915 }
916
917 Lexer.Lex();
918 return false;
919}
920
921/// ParseDirectiveSpace
922/// ::= .space expression [ , expression ]
923bool AsmParser::ParseDirectiveSpace() {
924 int64_t NumBytes;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000925 if (ParseAbsoluteExpression(NumBytes))
Daniel Dunbara0d14262009-06-24 23:30:00 +0000926 return true;
927
928 int64_t FillExpr = 0;
929 bool HasFillExpr = false;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000930 if (Lexer.isNot(AsmToken::EndOfStatement)) {
931 if (Lexer.isNot(AsmToken::Comma))
Daniel Dunbara0d14262009-06-24 23:30:00 +0000932 return TokError("unexpected token in '.space' directive");
933 Lexer.Lex();
934
Daniel Dunbar475839e2009-06-29 20:37:27 +0000935 if (ParseAbsoluteExpression(FillExpr))
Daniel Dunbara0d14262009-06-24 23:30:00 +0000936 return true;
937
938 HasFillExpr = true;
939
Daniel Dunbar3f872332009-07-28 16:08:33 +0000940 if (Lexer.isNot(AsmToken::EndOfStatement))
Daniel Dunbara0d14262009-06-24 23:30:00 +0000941 return TokError("unexpected token in '.space' directive");
942 }
943
944 Lexer.Lex();
945
946 if (NumBytes <= 0)
947 return TokError("invalid number of bytes in '.space' directive");
948
949 // FIXME: Sometimes the fill expr is 'nop' if it isn't supplied, instead of 0.
950 for (uint64_t i = 0, e = NumBytes; i != e; ++i)
951 Out.EmitValue(MCValue::get(FillExpr), 1);
952
953 return false;
954}
955
956/// ParseDirectiveFill
957/// ::= .fill expression , expression , expression
958bool AsmParser::ParseDirectiveFill() {
959 int64_t NumValues;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000960 if (ParseAbsoluteExpression(NumValues))
Daniel Dunbara0d14262009-06-24 23:30:00 +0000961 return true;
962
Daniel Dunbar3f872332009-07-28 16:08:33 +0000963 if (Lexer.isNot(AsmToken::Comma))
Daniel Dunbara0d14262009-06-24 23:30:00 +0000964 return TokError("unexpected token in '.fill' directive");
965 Lexer.Lex();
966
967 int64_t FillSize;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000968 if (ParseAbsoluteExpression(FillSize))
Daniel Dunbara0d14262009-06-24 23:30:00 +0000969 return true;
970
Daniel Dunbar3f872332009-07-28 16:08:33 +0000971 if (Lexer.isNot(AsmToken::Comma))
Daniel Dunbara0d14262009-06-24 23:30:00 +0000972 return TokError("unexpected token in '.fill' directive");
973 Lexer.Lex();
974
975 int64_t FillExpr;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000976 if (ParseAbsoluteExpression(FillExpr))
Daniel Dunbara0d14262009-06-24 23:30:00 +0000977 return true;
978
Daniel Dunbar3f872332009-07-28 16:08:33 +0000979 if (Lexer.isNot(AsmToken::EndOfStatement))
Daniel Dunbara0d14262009-06-24 23:30:00 +0000980 return TokError("unexpected token in '.fill' directive");
981
982 Lexer.Lex();
983
Daniel Dunbarbc38ca72009-08-21 15:43:35 +0000984 if (FillSize != 1 && FillSize != 2 && FillSize != 4 && FillSize != 8)
985 return TokError("invalid '.fill' size, expected 1, 2, 4, or 8");
Daniel Dunbara0d14262009-06-24 23:30:00 +0000986
987 for (uint64_t i = 0, e = NumValues; i != e; ++i)
988 Out.EmitValue(MCValue::get(FillExpr), FillSize);
989
990 return false;
991}
Daniel Dunbarc238b582009-06-25 22:44:51 +0000992
993/// ParseDirectiveOrg
994/// ::= .org expression [ , expression ]
995bool AsmParser::ParseDirectiveOrg() {
Daniel Dunbarf4b830f2009-06-30 02:10:03 +0000996 MCValue Offset;
997 if (ParseRelocatableExpression(Offset))
Daniel Dunbarc238b582009-06-25 22:44:51 +0000998 return true;
999
1000 // Parse optional fill expression.
1001 int64_t FillExpr = 0;
Daniel Dunbar3f872332009-07-28 16:08:33 +00001002 if (Lexer.isNot(AsmToken::EndOfStatement)) {
1003 if (Lexer.isNot(AsmToken::Comma))
Daniel Dunbarc238b582009-06-25 22:44:51 +00001004 return TokError("unexpected token in '.org' directive");
1005 Lexer.Lex();
1006
Daniel Dunbar475839e2009-06-29 20:37:27 +00001007 if (ParseAbsoluteExpression(FillExpr))
Daniel Dunbarc238b582009-06-25 22:44:51 +00001008 return true;
1009
Daniel Dunbar3f872332009-07-28 16:08:33 +00001010 if (Lexer.isNot(AsmToken::EndOfStatement))
Daniel Dunbarc238b582009-06-25 22:44:51 +00001011 return TokError("unexpected token in '.org' directive");
1012 }
1013
1014 Lexer.Lex();
Daniel Dunbarf4b830f2009-06-30 02:10:03 +00001015
1016 // FIXME: Only limited forms of relocatable expressions are accepted here, it
1017 // has to be relative to the current section.
1018 Out.EmitValueToOffset(Offset, FillExpr);
Daniel Dunbarc238b582009-06-25 22:44:51 +00001019
1020 return false;
1021}
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001022
1023/// ParseDirectiveAlign
1024/// ::= {.align, ...} expression [ , expression [ , expression ]]
1025bool AsmParser::ParseDirectiveAlign(bool IsPow2, unsigned ValueSize) {
1026 int64_t Alignment;
1027 if (ParseAbsoluteExpression(Alignment))
1028 return true;
1029
1030 SMLoc MaxBytesLoc;
1031 bool HasFillExpr = false;
1032 int64_t FillExpr = 0;
1033 int64_t MaxBytesToFill = 0;
Daniel Dunbar3f872332009-07-28 16:08:33 +00001034 if (Lexer.isNot(AsmToken::EndOfStatement)) {
1035 if (Lexer.isNot(AsmToken::Comma))
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001036 return TokError("unexpected token in directive");
1037 Lexer.Lex();
1038
1039 // The fill expression can be omitted while specifying a maximum number of
1040 // alignment bytes, e.g:
1041 // .align 3,,4
Daniel Dunbar3f872332009-07-28 16:08:33 +00001042 if (Lexer.isNot(AsmToken::Comma)) {
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001043 HasFillExpr = true;
1044 if (ParseAbsoluteExpression(FillExpr))
1045 return true;
1046 }
1047
Daniel Dunbar3f872332009-07-28 16:08:33 +00001048 if (Lexer.isNot(AsmToken::EndOfStatement)) {
1049 if (Lexer.isNot(AsmToken::Comma))
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001050 return TokError("unexpected token in directive");
1051 Lexer.Lex();
1052
1053 MaxBytesLoc = Lexer.getLoc();
1054 if (ParseAbsoluteExpression(MaxBytesToFill))
1055 return true;
1056
Daniel Dunbar3f872332009-07-28 16:08:33 +00001057 if (Lexer.isNot(AsmToken::EndOfStatement))
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001058 return TokError("unexpected token in directive");
1059 }
1060 }
1061
1062 Lexer.Lex();
1063
1064 if (!HasFillExpr) {
1065 // FIXME: Sometimes fill with nop.
1066 FillExpr = 0;
1067 }
1068
1069 // Compute alignment in bytes.
1070 if (IsPow2) {
1071 // FIXME: Diagnose overflow.
Chris Lattner39750252009-07-11 22:32:37 +00001072 Alignment = 1LL << Alignment;
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001073 }
1074
Daniel Dunbar0afb9f52009-08-21 23:01:53 +00001075 // Diagnose non-sensical max bytes to fill, which are treated as missing (this
1076 // matches 'as').
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001077 if (MaxBytesLoc.isValid()) {
1078 if (MaxBytesToFill < 1) {
Daniel Dunbar3fb76832009-06-30 00:49:23 +00001079 Warning(MaxBytesLoc, "alignment directive can never be satisfied in this "
Daniel Dunbar0afb9f52009-08-21 23:01:53 +00001080 "many bytes, ignoring maximum bytes expression");
1081 MaxBytesToFill = 0;
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001082 }
1083
1084 if (MaxBytesToFill >= Alignment) {
Daniel Dunbar3fb76832009-06-30 00:49:23 +00001085 Warning(MaxBytesLoc, "maximum bytes expression exceeds alignment and "
1086 "has no effect");
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001087 MaxBytesToFill = 0;
1088 }
1089 }
1090
1091 // FIXME: Target specific behavior about how the "extra" bytes are filled.
1092 Out.EmitValueToAlignment(Alignment, FillExpr, ValueSize, MaxBytesToFill);
1093
1094 return false;
1095}
1096
Daniel Dunbard7b267b2009-06-30 00:33:19 +00001097/// ParseDirectiveSymbolAttribute
1098/// ::= { ".globl", ".weak", ... } [ identifier ( , identifier )* ]
1099bool AsmParser::ParseDirectiveSymbolAttribute(MCStreamer::SymbolAttr Attr) {
Daniel Dunbar3f872332009-07-28 16:08:33 +00001100 if (Lexer.isNot(AsmToken::EndOfStatement)) {
Daniel Dunbard7b267b2009-06-30 00:33:19 +00001101 for (;;) {
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00001102 StringRef Name;
1103
1104 if (ParseIdentifier(Name))
Daniel Dunbard7b267b2009-06-30 00:33:19 +00001105 return TokError("expected identifier in directive");
1106
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00001107 MCSymbol *Sym = Ctx.GetOrCreateSymbol(Name);
Daniel Dunbard7b267b2009-06-30 00:33:19 +00001108
1109 // If this is use of an undefined symbol then mark it external.
1110 if (!Sym->getSection() && !Ctx.GetSymbolValue(Sym))
1111 Sym->setExternal(true);
1112
1113 Out.EmitSymbolAttribute(Sym, Attr);
1114
Daniel Dunbar3f872332009-07-28 16:08:33 +00001115 if (Lexer.is(AsmToken::EndOfStatement))
Daniel Dunbard7b267b2009-06-30 00:33:19 +00001116 break;
1117
Daniel Dunbar3f872332009-07-28 16:08:33 +00001118 if (Lexer.isNot(AsmToken::Comma))
Daniel Dunbard7b267b2009-06-30 00:33:19 +00001119 return TokError("unexpected token in directive");
1120 Lexer.Lex();
1121 }
1122 }
1123
1124 Lexer.Lex();
1125 return false;
1126}
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001127
Kevin Enderby95cf30c2009-07-14 18:17:10 +00001128/// ParseDirectiveDarwinSymbolDesc
1129/// ::= .desc identifier , expression
1130bool AsmParser::ParseDirectiveDarwinSymbolDesc() {
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00001131 StringRef Name;
1132 if (ParseIdentifier(Name))
Kevin Enderby95cf30c2009-07-14 18:17:10 +00001133 return TokError("expected identifier in directive");
1134
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00001135 // Handle the identifier as the key symbol.
1136 MCSymbol *Sym = Ctx.GetOrCreateSymbol(Name);
Kevin Enderby95cf30c2009-07-14 18:17:10 +00001137
Daniel Dunbar3f872332009-07-28 16:08:33 +00001138 if (Lexer.isNot(AsmToken::Comma))
Kevin Enderby95cf30c2009-07-14 18:17:10 +00001139 return TokError("unexpected token in '.desc' directive");
1140 Lexer.Lex();
1141
1142 SMLoc DescLoc = Lexer.getLoc();
1143 int64_t DescValue;
1144 if (ParseAbsoluteExpression(DescValue))
1145 return true;
1146
Daniel Dunbar3f872332009-07-28 16:08:33 +00001147 if (Lexer.isNot(AsmToken::EndOfStatement))
Kevin Enderby95cf30c2009-07-14 18:17:10 +00001148 return TokError("unexpected token in '.desc' directive");
1149
1150 Lexer.Lex();
1151
1152 // Set the n_desc field of this Symbol to this DescValue
1153 Out.EmitSymbolDesc(Sym, DescValue);
1154
1155 return false;
1156}
1157
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001158/// ParseDirectiveComm
Chris Lattner1fc3d752009-07-09 17:25:12 +00001159/// ::= ( .comm | .lcomm ) identifier , size_expression [ , align_expression ]
1160bool AsmParser::ParseDirectiveComm(bool IsLocal) {
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00001161 SMLoc IDLoc = Lexer.getLoc();
1162 StringRef Name;
1163 if (ParseIdentifier(Name))
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001164 return TokError("expected identifier in directive");
1165
Daniel Dunbar76c4d762009-07-31 21:55:09 +00001166 // Handle the identifier as the key symbol.
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00001167 MCSymbol *Sym = Ctx.GetOrCreateSymbol(Name);
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001168
Daniel Dunbar3f872332009-07-28 16:08:33 +00001169 if (Lexer.isNot(AsmToken::Comma))
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001170 return TokError("unexpected token in directive");
1171 Lexer.Lex();
1172
1173 int64_t Size;
1174 SMLoc SizeLoc = Lexer.getLoc();
1175 if (ParseAbsoluteExpression(Size))
1176 return true;
1177
1178 int64_t Pow2Alignment = 0;
1179 SMLoc Pow2AlignmentLoc;
Daniel Dunbar3f872332009-07-28 16:08:33 +00001180 if (Lexer.is(AsmToken::Comma)) {
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001181 Lexer.Lex();
1182 Pow2AlignmentLoc = Lexer.getLoc();
1183 if (ParseAbsoluteExpression(Pow2Alignment))
1184 return true;
1185 }
1186
Daniel Dunbar3f872332009-07-28 16:08:33 +00001187 if (Lexer.isNot(AsmToken::EndOfStatement))
Chris Lattner1fc3d752009-07-09 17:25:12 +00001188 return TokError("unexpected token in '.comm' or '.lcomm' directive");
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001189
1190 Lexer.Lex();
1191
Chris Lattner1fc3d752009-07-09 17:25:12 +00001192 // NOTE: a size of zero for a .comm should create a undefined symbol
1193 // but a size of .lcomm creates a bss symbol of size zero.
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001194 if (Size < 0)
Chris Lattner1fc3d752009-07-09 17:25:12 +00001195 return Error(SizeLoc, "invalid '.comm' or '.lcomm' directive size, can't "
1196 "be less than zero");
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001197
1198 // NOTE: The alignment in the directive is a power of 2 value, the assember
1199 // may internally end up wanting an alignment in bytes.
1200 // FIXME: Diagnose overflow.
1201 if (Pow2Alignment < 0)
Chris Lattner1fc3d752009-07-09 17:25:12 +00001202 return Error(Pow2AlignmentLoc, "invalid '.comm' or '.lcomm' directive "
1203 "alignment, can't be less than zero");
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001204
1205 // TODO: Symbol must be undefined or it is a error to re-defined the symbol
1206 if (Sym->getSection() || Ctx.GetSymbolValue(Sym))
1207 return Error(IDLoc, "invalid symbol redefinition");
1208
Chris Lattner1fc3d752009-07-09 17:25:12 +00001209 // Create the Symbol as a common or local common with Size and Pow2Alignment
1210 Out.EmitCommonSymbol(Sym, Size, Pow2Alignment, IsLocal);
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001211
1212 return false;
1213}
Chris Lattner9be3fee2009-07-10 22:20:30 +00001214
1215/// ParseDirectiveDarwinZerofill
1216/// ::= .zerofill segname , sectname [, identifier , size_expression [
1217/// , align_expression ]]
1218bool AsmParser::ParseDirectiveDarwinZerofill() {
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00001219 // FIXME: Handle quoted names here.
1220
Daniel Dunbar3f872332009-07-28 16:08:33 +00001221 if (Lexer.isNot(AsmToken::Identifier))
Chris Lattner9be3fee2009-07-10 22:20:30 +00001222 return TokError("expected segment name after '.zerofill' directive");
Chris Lattnerff4bc462009-08-10 01:39:42 +00001223 StringRef Segment = Lexer.getTok().getString();
Chris Lattner9be3fee2009-07-10 22:20:30 +00001224 Lexer.Lex();
1225
Daniel Dunbar3f872332009-07-28 16:08:33 +00001226 if (Lexer.isNot(AsmToken::Comma))
Chris Lattner9be3fee2009-07-10 22:20:30 +00001227 return TokError("unexpected token in directive");
Chris Lattner9be3fee2009-07-10 22:20:30 +00001228 Lexer.Lex();
1229
Daniel Dunbar3f872332009-07-28 16:08:33 +00001230 if (Lexer.isNot(AsmToken::Identifier))
Chris Lattner9be3fee2009-07-10 22:20:30 +00001231 return TokError("expected section name after comma in '.zerofill' "
1232 "directive");
Chris Lattnerff4bc462009-08-10 01:39:42 +00001233 StringRef Section = Lexer.getTok().getString();
Chris Lattner9be3fee2009-07-10 22:20:30 +00001234 Lexer.Lex();
1235
Chris Lattner9be3fee2009-07-10 22:20:30 +00001236 // If this is the end of the line all that was wanted was to create the
1237 // the section but with no symbol.
Daniel Dunbar3f872332009-07-28 16:08:33 +00001238 if (Lexer.is(AsmToken::EndOfStatement)) {
Chris Lattnerff4bc462009-08-10 01:39:42 +00001239 // FIXME: CACHE THIS.
1240 MCSection *S = 0; //Ctx.GetSection(Section);
Chris Lattner6bdd74c2009-07-31 18:27:48 +00001241 if (S == 0)
Chris Lattnerff4bc462009-08-10 01:39:42 +00001242 S = MCSectionMachO::Create(Segment, Section,
1243 MCSectionMachO::S_ZEROFILL, 0,
1244 SectionKind(), Ctx);
Chris Lattner6bdd74c2009-07-31 18:27:48 +00001245
Chris Lattner9be3fee2009-07-10 22:20:30 +00001246 // Create the zerofill section but no symbol
Chris Lattner6bdd74c2009-07-31 18:27:48 +00001247 Out.EmitZerofill(S);
Chris Lattner9be3fee2009-07-10 22:20:30 +00001248 return false;
1249 }
1250
Daniel Dunbar3f872332009-07-28 16:08:33 +00001251 if (Lexer.isNot(AsmToken::Comma))
Chris Lattner9be3fee2009-07-10 22:20:30 +00001252 return TokError("unexpected token in directive");
1253 Lexer.Lex();
1254
Daniel Dunbar3f872332009-07-28 16:08:33 +00001255 if (Lexer.isNot(AsmToken::Identifier))
Chris Lattner9be3fee2009-07-10 22:20:30 +00001256 return TokError("expected identifier in directive");
1257
1258 // handle the identifier as the key symbol.
1259 SMLoc IDLoc = Lexer.getLoc();
Daniel Dunbar419aded2009-07-28 16:38:40 +00001260 MCSymbol *Sym = Ctx.GetOrCreateSymbol(Lexer.getTok().getString());
Chris Lattner9be3fee2009-07-10 22:20:30 +00001261 Lexer.Lex();
1262
Daniel Dunbar3f872332009-07-28 16:08:33 +00001263 if (Lexer.isNot(AsmToken::Comma))
Chris Lattner9be3fee2009-07-10 22:20:30 +00001264 return TokError("unexpected token in directive");
1265 Lexer.Lex();
1266
1267 int64_t Size;
1268 SMLoc SizeLoc = Lexer.getLoc();
1269 if (ParseAbsoluteExpression(Size))
1270 return true;
1271
1272 int64_t Pow2Alignment = 0;
1273 SMLoc Pow2AlignmentLoc;
Daniel Dunbar3f872332009-07-28 16:08:33 +00001274 if (Lexer.is(AsmToken::Comma)) {
Chris Lattner9be3fee2009-07-10 22:20:30 +00001275 Lexer.Lex();
1276 Pow2AlignmentLoc = Lexer.getLoc();
1277 if (ParseAbsoluteExpression(Pow2Alignment))
1278 return true;
1279 }
1280
Daniel Dunbar3f872332009-07-28 16:08:33 +00001281 if (Lexer.isNot(AsmToken::EndOfStatement))
Chris Lattner9be3fee2009-07-10 22:20:30 +00001282 return TokError("unexpected token in '.zerofill' directive");
1283
1284 Lexer.Lex();
1285
1286 if (Size < 0)
1287 return Error(SizeLoc, "invalid '.zerofill' directive size, can't be less "
1288 "than zero");
1289
1290 // NOTE: The alignment in the directive is a power of 2 value, the assember
1291 // may internally end up wanting an alignment in bytes.
1292 // FIXME: Diagnose overflow.
1293 if (Pow2Alignment < 0)
1294 return Error(Pow2AlignmentLoc, "invalid '.zerofill' directive alignment, "
1295 "can't be less than zero");
1296
1297 // TODO: Symbol must be undefined or it is a error to re-defined the symbol
1298 if (Sym->getSection() || Ctx.GetSymbolValue(Sym))
1299 return Error(IDLoc, "invalid symbol redefinition");
1300
Chris Lattner6bdd74c2009-07-31 18:27:48 +00001301 // FIXME: Arch specific.
Chris Lattnerff4bc462009-08-10 01:39:42 +00001302 // FIXME: CACHE.
1303 MCSection *S = 0; //Ctx.GetSection(Section);
Chris Lattner6bdd74c2009-07-31 18:27:48 +00001304 if (S == 0)
Chris Lattnerff4bc462009-08-10 01:39:42 +00001305 S = MCSectionMachO::Create(Segment, Section,
1306 MCSectionMachO::S_ZEROFILL, 0,
1307 SectionKind(), Ctx);
Chris Lattner6bdd74c2009-07-31 18:27:48 +00001308
Chris Lattner9be3fee2009-07-10 22:20:30 +00001309 // Create the zerofill Symbol with Size and Pow2Alignment
Chris Lattner6bdd74c2009-07-31 18:27:48 +00001310 Out.EmitZerofill(S, Sym, Size, Pow2Alignment);
Chris Lattner9be3fee2009-07-10 22:20:30 +00001311
1312 return false;
1313}
Kevin Enderbya5c78322009-07-13 21:03:15 +00001314
1315/// ParseDirectiveDarwinSubsectionsViaSymbols
1316/// ::= .subsections_via_symbols
1317bool AsmParser::ParseDirectiveDarwinSubsectionsViaSymbols() {
Daniel Dunbar3f872332009-07-28 16:08:33 +00001318 if (Lexer.isNot(AsmToken::EndOfStatement))
Kevin Enderbya5c78322009-07-13 21:03:15 +00001319 return TokError("unexpected token in '.subsections_via_symbols' directive");
1320
1321 Lexer.Lex();
1322
Kevin Enderbyf96db462009-07-16 17:56:39 +00001323 Out.EmitAssemblerFlag(MCStreamer::SubsectionsViaSymbols);
Kevin Enderbya5c78322009-07-13 21:03:15 +00001324
1325 return false;
1326}
Kevin Enderby5f1f0b82009-07-13 23:15:14 +00001327
1328/// ParseDirectiveAbort
1329/// ::= .abort [ "abort_string" ]
1330bool AsmParser::ParseDirectiveAbort() {
Daniel Dunbarf9507ff2009-07-27 23:20:52 +00001331 // FIXME: Use loc from directive.
1332 SMLoc Loc = Lexer.getLoc();
1333
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001334 StringRef Str = "";
Daniel Dunbar3f872332009-07-28 16:08:33 +00001335 if (Lexer.isNot(AsmToken::EndOfStatement)) {
1336 if (Lexer.isNot(AsmToken::String))
Kevin Enderby5f1f0b82009-07-13 23:15:14 +00001337 return TokError("expected string in '.abort' directive");
1338
Daniel Dunbar419aded2009-07-28 16:38:40 +00001339 Str = Lexer.getTok().getString();
Kevin Enderby5f1f0b82009-07-13 23:15:14 +00001340
1341 Lexer.Lex();
1342 }
1343
Daniel Dunbar3f872332009-07-28 16:08:33 +00001344 if (Lexer.isNot(AsmToken::EndOfStatement))
Kevin Enderby5f1f0b82009-07-13 23:15:14 +00001345 return TokError("unexpected token in '.abort' directive");
1346
1347 Lexer.Lex();
1348
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001349 // FIXME: Handle here.
Daniel Dunbarf9507ff2009-07-27 23:20:52 +00001350 if (Str.empty())
1351 Error(Loc, ".abort detected. Assembly stopping.");
1352 else
1353 Error(Loc, ".abort '" + Str + "' detected. Assembly stopping.");
Kevin Enderby5f1f0b82009-07-13 23:15:14 +00001354
1355 return false;
1356}
Kevin Enderby71148242009-07-14 21:35:03 +00001357
1358/// ParseDirectiveLsym
1359/// ::= .lsym identifier , expression
1360bool AsmParser::ParseDirectiveDarwinLsym() {
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00001361 StringRef Name;
1362 if (ParseIdentifier(Name))
Kevin Enderby71148242009-07-14 21:35:03 +00001363 return TokError("expected identifier in directive");
1364
Daniel Dunbar76c4d762009-07-31 21:55:09 +00001365 // Handle the identifier as the key symbol.
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00001366 MCSymbol *Sym = Ctx.GetOrCreateSymbol(Name);
Kevin Enderby71148242009-07-14 21:35:03 +00001367
Daniel Dunbar3f872332009-07-28 16:08:33 +00001368 if (Lexer.isNot(AsmToken::Comma))
Kevin Enderby71148242009-07-14 21:35:03 +00001369 return TokError("unexpected token in '.lsym' directive");
1370 Lexer.Lex();
1371
1372 MCValue Expr;
1373 if (ParseRelocatableExpression(Expr))
1374 return true;
1375
Daniel Dunbar3f872332009-07-28 16:08:33 +00001376 if (Lexer.isNot(AsmToken::EndOfStatement))
Kevin Enderby71148242009-07-14 21:35:03 +00001377 return TokError("unexpected token in '.lsym' directive");
1378
1379 Lexer.Lex();
1380
1381 // Create the Sym with the value of the Expr
1382 Out.EmitLocalSymbol(Sym, Expr);
1383
1384 return false;
1385}
Kevin Enderby1f049b22009-07-14 23:21:55 +00001386
1387/// ParseDirectiveInclude
1388/// ::= .include "filename"
1389bool AsmParser::ParseDirectiveInclude() {
Daniel Dunbar3f872332009-07-28 16:08:33 +00001390 if (Lexer.isNot(AsmToken::String))
Kevin Enderby1f049b22009-07-14 23:21:55 +00001391 return TokError("expected string in '.include' directive");
1392
Daniel Dunbar419aded2009-07-28 16:38:40 +00001393 std::string Filename = Lexer.getTok().getString();
Chris Lattner8e25e2d2009-07-16 06:14:39 +00001394 SMLoc IncludeLoc = Lexer.getLoc();
Kevin Enderby1f049b22009-07-14 23:21:55 +00001395 Lexer.Lex();
1396
Daniel Dunbar3f872332009-07-28 16:08:33 +00001397 if (Lexer.isNot(AsmToken::EndOfStatement))
Kevin Enderby1f049b22009-07-14 23:21:55 +00001398 return TokError("unexpected token in '.include' directive");
1399
Chris Lattner8e25e2d2009-07-16 06:14:39 +00001400 // Strip the quotes.
1401 Filename = Filename.substr(1, Filename.size()-2);
1402
1403 // Attempt to switch the lexer to the included file before consuming the end
1404 // of statement to avoid losing it when we switch.
1405 if (Lexer.EnterIncludeFile(Filename)) {
1406 Lexer.PrintMessage(IncludeLoc,
1407 "Could not find include file '" + Filename + "'",
1408 "error");
1409 return true;
1410 }
Kevin Enderby1f049b22009-07-14 23:21:55 +00001411
1412 return false;
1413}
Kevin Enderby6e68cd92009-07-15 15:30:11 +00001414
1415/// ParseDirectiveDarwinDumpOrLoad
1416/// ::= ( .dump | .load ) "filename"
Kevin Enderby5026ae42009-07-20 20:25:37 +00001417bool AsmParser::ParseDirectiveDarwinDumpOrLoad(SMLoc IDLoc, bool IsDump) {
Daniel Dunbar3f872332009-07-28 16:08:33 +00001418 if (Lexer.isNot(AsmToken::String))
Kevin Enderby6e68cd92009-07-15 15:30:11 +00001419 return TokError("expected string in '.dump' or '.load' directive");
1420
Kevin Enderby6e68cd92009-07-15 15:30:11 +00001421 Lexer.Lex();
1422
Daniel Dunbar3f872332009-07-28 16:08:33 +00001423 if (Lexer.isNot(AsmToken::EndOfStatement))
Kevin Enderby6e68cd92009-07-15 15:30:11 +00001424 return TokError("unexpected token in '.dump' or '.load' directive");
1425
1426 Lexer.Lex();
1427
Kevin Enderby5026ae42009-07-20 20:25:37 +00001428 // FIXME: If/when .dump and .load are implemented they will be done in the
1429 // the assembly parser and not have any need for an MCStreamer API.
Kevin Enderby6e68cd92009-07-15 15:30:11 +00001430 if (IsDump)
Kevin Enderby5026ae42009-07-20 20:25:37 +00001431 Warning(IDLoc, "ignoring directive .dump for now");
Kevin Enderby6e68cd92009-07-15 15:30:11 +00001432 else
Kevin Enderby5026ae42009-07-20 20:25:37 +00001433 Warning(IDLoc, "ignoring directive .load for now");
Kevin Enderby6e68cd92009-07-15 15:30:11 +00001434
1435 return false;
1436}
Kevin Enderbyc114ed72009-08-07 22:46:00 +00001437
1438/// ParseDirectiveIf
1439/// ::= .if expression
1440bool AsmParser::ParseDirectiveIf(SMLoc DirectiveLoc) {
1441 // Consume the identifier that was the .if directive
1442 Lexer.Lex();
1443
1444 TheCondStack.push_back(TheCondState);
1445 TheCondState.TheCond = AsmCond::IfCond;
1446 if(TheCondState.Ignore) {
1447 EatToEndOfStatement();
1448 }
1449 else {
1450 int64_t ExprValue;
1451 if (ParseAbsoluteExpression(ExprValue))
1452 return true;
1453
1454 if (Lexer.isNot(AsmToken::EndOfStatement))
1455 return TokError("unexpected token in '.if' directive");
1456
1457 Lexer.Lex();
1458
1459 TheCondState.CondMet = ExprValue;
1460 TheCondState.Ignore = !TheCondState.CondMet;
1461 }
1462
1463 return false;
1464}
1465
1466/// ParseDirectiveElseIf
1467/// ::= .elseif expression
1468bool AsmParser::ParseDirectiveElseIf(SMLoc DirectiveLoc) {
1469 if (TheCondState.TheCond != AsmCond::IfCond &&
1470 TheCondState.TheCond != AsmCond::ElseIfCond)
1471 Error(DirectiveLoc, "Encountered a .elseif that doesn't follow a .if or "
1472 " an .elseif");
1473 TheCondState.TheCond = AsmCond::ElseIfCond;
1474
1475 // Consume the identifier that was the .elseif directive
1476 Lexer.Lex();
1477
1478 bool LastIgnoreState = false;
1479 if (!TheCondStack.empty())
1480 LastIgnoreState = TheCondStack.back().Ignore;
1481 if (LastIgnoreState || TheCondState.CondMet) {
1482 TheCondState.Ignore = true;
1483 EatToEndOfStatement();
1484 }
1485 else {
1486 int64_t ExprValue;
1487 if (ParseAbsoluteExpression(ExprValue))
1488 return true;
1489
1490 if (Lexer.isNot(AsmToken::EndOfStatement))
1491 return TokError("unexpected token in '.elseif' directive");
1492
1493 Lexer.Lex();
1494 TheCondState.CondMet = ExprValue;
1495 TheCondState.Ignore = !TheCondState.CondMet;
1496 }
1497
1498 return false;
1499}
1500
1501/// ParseDirectiveElse
1502/// ::= .else
1503bool AsmParser::ParseDirectiveElse(SMLoc DirectiveLoc) {
1504 // Consume the identifier that was the .else directive
1505 Lexer.Lex();
1506
1507 if (Lexer.isNot(AsmToken::EndOfStatement))
1508 return TokError("unexpected token in '.else' directive");
1509
1510 Lexer.Lex();
1511
1512 if (TheCondState.TheCond != AsmCond::IfCond &&
1513 TheCondState.TheCond != AsmCond::ElseIfCond)
1514 Error(DirectiveLoc, "Encountered a .else that doesn't follow a .if or an "
1515 ".elseif");
1516 TheCondState.TheCond = AsmCond::ElseCond;
1517 bool LastIgnoreState = false;
1518 if (!TheCondStack.empty())
1519 LastIgnoreState = TheCondStack.back().Ignore;
1520 if (LastIgnoreState || TheCondState.CondMet)
1521 TheCondState.Ignore = true;
1522 else
1523 TheCondState.Ignore = false;
1524
1525 return false;
1526}
1527
1528/// ParseDirectiveEndIf
1529/// ::= .endif
1530bool AsmParser::ParseDirectiveEndIf(SMLoc DirectiveLoc) {
1531 // Consume the identifier that was the .endif directive
1532 Lexer.Lex();
1533
1534 if (Lexer.isNot(AsmToken::EndOfStatement))
1535 return TokError("unexpected token in '.endif' directive");
1536
1537 Lexer.Lex();
1538
1539 if ((TheCondState.TheCond == AsmCond::NoCond) ||
1540 TheCondStack.empty())
1541 Error(DirectiveLoc, "Encountered a .endif that doesn't follow a .if or "
1542 ".else");
1543 if (!TheCondStack.empty()) {
1544 TheCondState = TheCondStack.back();
1545 TheCondStack.pop_back();
1546 }
1547
1548 return false;
1549}
Daniel Dunbard0c14d62009-08-11 04:24:50 +00001550
1551/// ParseDirectiveFile
1552/// ::= .file [number] string
1553bool AsmParser::ParseDirectiveFile(SMLoc DirectiveLoc) {
1554 // FIXME: I'm not sure what this is.
1555 int64_t FileNumber = -1;
1556 if (Lexer.is(AsmToken::Integer)) {
1557 FileNumber = Lexer.getTok().getIntVal();
1558 Lexer.Lex();
1559
1560 if (FileNumber < 1)
1561 return TokError("file number less than one");
1562 }
1563
1564 if (Lexer.isNot(AsmToken::String))
1565 return TokError("unexpected token in '.file' directive");
1566
1567 StringRef FileName = Lexer.getTok().getString();
1568 Lexer.Lex();
1569
1570 if (Lexer.isNot(AsmToken::EndOfStatement))
1571 return TokError("unexpected token in '.file' directive");
1572
1573 // FIXME: Do something with the .file.
1574
1575 return false;
1576}
1577
1578/// ParseDirectiveLine
1579/// ::= .line [number]
1580bool AsmParser::ParseDirectiveLine(SMLoc DirectiveLoc) {
1581 if (Lexer.isNot(AsmToken::EndOfStatement)) {
1582 if (Lexer.isNot(AsmToken::Integer))
1583 return TokError("unexpected token in '.line' directive");
1584
1585 int64_t LineNumber = Lexer.getTok().getIntVal();
1586 (void) LineNumber;
1587 Lexer.Lex();
1588
1589 // FIXME: Do something with the .line.
1590 }
1591
1592 if (Lexer.isNot(AsmToken::EndOfStatement))
1593 return TokError("unexpected token in '.file' directive");
1594
1595 return false;
1596}
1597
1598
1599/// ParseDirectiveLoc
1600/// ::= .loc number [number [number]]
1601bool AsmParser::ParseDirectiveLoc(SMLoc DirectiveLoc) {
1602 if (Lexer.isNot(AsmToken::Integer))
1603 return TokError("unexpected token in '.loc' directive");
1604
1605 // FIXME: What are these fields?
1606 int64_t FileNumber = Lexer.getTok().getIntVal();
1607 (void) FileNumber;
1608 // FIXME: Validate file.
1609
1610 Lexer.Lex();
1611 if (Lexer.isNot(AsmToken::EndOfStatement)) {
1612 if (Lexer.isNot(AsmToken::Integer))
1613 return TokError("unexpected token in '.loc' directive");
1614
1615 int64_t Param2 = Lexer.getTok().getIntVal();
1616 (void) Param2;
1617 Lexer.Lex();
1618
1619 if (Lexer.isNot(AsmToken::EndOfStatement)) {
1620 if (Lexer.isNot(AsmToken::Integer))
1621 return TokError("unexpected token in '.loc' directive");
1622
1623 int64_t Param3 = Lexer.getTok().getIntVal();
1624 (void) Param3;
1625 Lexer.Lex();
1626
1627 // FIXME: Do something with the .loc.
1628 }
1629 }
1630
1631 if (Lexer.isNot(AsmToken::EndOfStatement))
1632 return TokError("unexpected token in '.file' directive");
1633
1634 return false;
1635}
1636