blob: 19781212976c0541e679c8997b6a16943daaea10 [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!
797 MCSection *S = 0; // Ctx.GetSection(Section);
Chris Lattner56594f92009-07-31 17:47:16 +0000798 if (S == 0)
Chris Lattnerff4bc462009-08-10 01:39:42 +0000799 S = MCSectionMachO::Create(Segment, Section, TAA, StubSize,
800 SectionKind(), Ctx);
Chris Lattner56594f92009-07-31 17:47:16 +0000801
802 Out.SwitchSection(S);
Daniel Dunbar2330df62009-08-21 23:30:15 +0000803
804 // Set the implicit alignment, if any.
805 //
806 // FIXME: This isn't really what 'as' does; I think it just uses the implicit
807 // alignment on the section (e.g., if one manually inserts bytes into the
808 // section, then just issueing the section switch directive will not realign
809 // the section. However, this is arguably more reasonable behavior, and there
810 // is no good reason for someone to intentionally emit incorrectly sized
811 // values into the implicitly aligned sections.
812 if (Align)
813 Out.EmitValueToAlignment(Align, 0, 1, 0);
814
Chris Lattner529fb542009-06-24 05:13:15 +0000815 return false;
816}
Daniel Dunbara0d14262009-06-24 23:30:00 +0000817
Daniel Dunbar1ab75942009-08-14 18:19:52 +0000818bool AsmParser::ParseEscapedString(std::string &Data) {
819 assert(Lexer.is(AsmToken::String) && "Unexpected current token!");
820
821 Data = "";
822 StringRef Str = Lexer.getTok().getStringContents();
823 for (unsigned i = 0, e = Str.size(); i != e; ++i) {
824 if (Str[i] != '\\') {
825 Data += Str[i];
826 continue;
827 }
828
829 // Recognize escaped characters. Note that this escape semantics currently
830 // loosely follows Darwin 'as'. Notably, it doesn't support hex escapes.
831 ++i;
832 if (i == e)
833 return TokError("unexpected backslash at end of string");
834
835 // Recognize octal sequences.
836 if ((unsigned) (Str[i] - '0') <= 7) {
837 // Consume up to three octal characters.
838 unsigned Value = Str[i] - '0';
839
840 if (i + 1 != e && ((unsigned) (Str[i + 1] - '0')) <= 7) {
841 ++i;
842 Value = Value * 8 + (Str[i] - '0');
843
844 if (i + 1 != e && ((unsigned) (Str[i + 1] - '0')) <= 7) {
845 ++i;
846 Value = Value * 8 + (Str[i] - '0');
847 }
848 }
849
850 if (Value > 255)
851 return TokError("invalid octal escape sequence (out of range)");
852
853 Data += (unsigned char) Value;
854 continue;
855 }
856
857 // Otherwise recognize individual escapes.
858 switch (Str[i]) {
859 default:
860 // Just reject invalid escape sequences for now.
861 return TokError("invalid escape sequence (unrecognized character)");
862
863 case 'b': Data += '\b'; break;
864 case 'f': Data += '\f'; break;
865 case 'n': Data += '\n'; break;
866 case 'r': Data += '\r'; break;
867 case 't': Data += '\t'; break;
868 case '"': Data += '"'; break;
869 case '\\': Data += '\\'; break;
870 }
871 }
872
873 return false;
874}
875
Daniel Dunbara0d14262009-06-24 23:30:00 +0000876/// ParseDirectiveAscii:
Daniel Dunbar475839e2009-06-29 20:37:27 +0000877/// ::= ( .ascii | .asciz ) [ "string" ( , "string" )* ]
Daniel Dunbara0d14262009-06-24 23:30:00 +0000878bool AsmParser::ParseDirectiveAscii(bool ZeroTerminated) {
Daniel Dunbar3f872332009-07-28 16:08:33 +0000879 if (Lexer.isNot(AsmToken::EndOfStatement)) {
Daniel Dunbara0d14262009-06-24 23:30:00 +0000880 for (;;) {
Daniel Dunbar3f872332009-07-28 16:08:33 +0000881 if (Lexer.isNot(AsmToken::String))
Daniel Dunbara0d14262009-06-24 23:30:00 +0000882 return TokError("expected string in '.ascii' or '.asciz' directive");
883
Daniel Dunbar1ab75942009-08-14 18:19:52 +0000884 std::string Data;
885 if (ParseEscapedString(Data))
886 return true;
887
888 Out.EmitBytes(Data);
Daniel Dunbara0d14262009-06-24 23:30:00 +0000889 if (ZeroTerminated)
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000890 Out.EmitBytes(StringRef("\0", 1));
Daniel Dunbara0d14262009-06-24 23:30:00 +0000891
892 Lexer.Lex();
893
Daniel Dunbar3f872332009-07-28 16:08:33 +0000894 if (Lexer.is(AsmToken::EndOfStatement))
Daniel Dunbara0d14262009-06-24 23:30:00 +0000895 break;
896
Daniel Dunbar3f872332009-07-28 16:08:33 +0000897 if (Lexer.isNot(AsmToken::Comma))
Daniel Dunbara0d14262009-06-24 23:30:00 +0000898 return TokError("unexpected token in '.ascii' or '.asciz' directive");
899 Lexer.Lex();
900 }
901 }
902
903 Lexer.Lex();
904 return false;
905}
906
907/// ParseDirectiveValue
908/// ::= (.byte | .short | ... ) [ expression (, expression)* ]
909bool AsmParser::ParseDirectiveValue(unsigned Size) {
Daniel Dunbar3f872332009-07-28 16:08:33 +0000910 if (Lexer.isNot(AsmToken::EndOfStatement)) {
Daniel Dunbara0d14262009-06-24 23:30:00 +0000911 for (;;) {
Daniel Dunbarf4b830f2009-06-30 02:10:03 +0000912 MCValue Expr;
913 if (ParseRelocatableExpression(Expr))
Daniel Dunbara0d14262009-06-24 23:30:00 +0000914 return true;
915
Daniel Dunbarf4b830f2009-06-30 02:10:03 +0000916 Out.EmitValue(Expr, Size);
Daniel Dunbara0d14262009-06-24 23:30:00 +0000917
Daniel Dunbar3f872332009-07-28 16:08:33 +0000918 if (Lexer.is(AsmToken::EndOfStatement))
Daniel Dunbara0d14262009-06-24 23:30:00 +0000919 break;
920
921 // FIXME: Improve diagnostic.
Daniel Dunbar3f872332009-07-28 16:08:33 +0000922 if (Lexer.isNot(AsmToken::Comma))
Daniel Dunbara0d14262009-06-24 23:30:00 +0000923 return TokError("unexpected token in directive");
924 Lexer.Lex();
925 }
926 }
927
928 Lexer.Lex();
929 return false;
930}
931
932/// ParseDirectiveSpace
933/// ::= .space expression [ , expression ]
934bool AsmParser::ParseDirectiveSpace() {
935 int64_t NumBytes;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000936 if (ParseAbsoluteExpression(NumBytes))
Daniel Dunbara0d14262009-06-24 23:30:00 +0000937 return true;
938
939 int64_t FillExpr = 0;
940 bool HasFillExpr = false;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000941 if (Lexer.isNot(AsmToken::EndOfStatement)) {
942 if (Lexer.isNot(AsmToken::Comma))
Daniel Dunbara0d14262009-06-24 23:30:00 +0000943 return TokError("unexpected token in '.space' directive");
944 Lexer.Lex();
945
Daniel Dunbar475839e2009-06-29 20:37:27 +0000946 if (ParseAbsoluteExpression(FillExpr))
Daniel Dunbara0d14262009-06-24 23:30:00 +0000947 return true;
948
949 HasFillExpr = true;
950
Daniel Dunbar3f872332009-07-28 16:08:33 +0000951 if (Lexer.isNot(AsmToken::EndOfStatement))
Daniel Dunbara0d14262009-06-24 23:30:00 +0000952 return TokError("unexpected token in '.space' directive");
953 }
954
955 Lexer.Lex();
956
957 if (NumBytes <= 0)
958 return TokError("invalid number of bytes in '.space' directive");
959
960 // FIXME: Sometimes the fill expr is 'nop' if it isn't supplied, instead of 0.
961 for (uint64_t i = 0, e = NumBytes; i != e; ++i)
962 Out.EmitValue(MCValue::get(FillExpr), 1);
963
964 return false;
965}
966
967/// ParseDirectiveFill
968/// ::= .fill expression , expression , expression
969bool AsmParser::ParseDirectiveFill() {
970 int64_t NumValues;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000971 if (ParseAbsoluteExpression(NumValues))
Daniel Dunbara0d14262009-06-24 23:30:00 +0000972 return true;
973
Daniel Dunbar3f872332009-07-28 16:08:33 +0000974 if (Lexer.isNot(AsmToken::Comma))
Daniel Dunbara0d14262009-06-24 23:30:00 +0000975 return TokError("unexpected token in '.fill' directive");
976 Lexer.Lex();
977
978 int64_t FillSize;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000979 if (ParseAbsoluteExpression(FillSize))
Daniel Dunbara0d14262009-06-24 23:30:00 +0000980 return true;
981
Daniel Dunbar3f872332009-07-28 16:08:33 +0000982 if (Lexer.isNot(AsmToken::Comma))
Daniel Dunbara0d14262009-06-24 23:30:00 +0000983 return TokError("unexpected token in '.fill' directive");
984 Lexer.Lex();
985
986 int64_t FillExpr;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000987 if (ParseAbsoluteExpression(FillExpr))
Daniel Dunbara0d14262009-06-24 23:30:00 +0000988 return true;
989
Daniel Dunbar3f872332009-07-28 16:08:33 +0000990 if (Lexer.isNot(AsmToken::EndOfStatement))
Daniel Dunbara0d14262009-06-24 23:30:00 +0000991 return TokError("unexpected token in '.fill' directive");
992
993 Lexer.Lex();
994
Daniel Dunbarbc38ca72009-08-21 15:43:35 +0000995 if (FillSize != 1 && FillSize != 2 && FillSize != 4 && FillSize != 8)
996 return TokError("invalid '.fill' size, expected 1, 2, 4, or 8");
Daniel Dunbara0d14262009-06-24 23:30:00 +0000997
998 for (uint64_t i = 0, e = NumValues; i != e; ++i)
999 Out.EmitValue(MCValue::get(FillExpr), FillSize);
1000
1001 return false;
1002}
Daniel Dunbarc238b582009-06-25 22:44:51 +00001003
1004/// ParseDirectiveOrg
1005/// ::= .org expression [ , expression ]
1006bool AsmParser::ParseDirectiveOrg() {
Daniel Dunbarf4b830f2009-06-30 02:10:03 +00001007 MCValue Offset;
1008 if (ParseRelocatableExpression(Offset))
Daniel Dunbarc238b582009-06-25 22:44:51 +00001009 return true;
1010
1011 // Parse optional fill expression.
1012 int64_t FillExpr = 0;
Daniel Dunbar3f872332009-07-28 16:08:33 +00001013 if (Lexer.isNot(AsmToken::EndOfStatement)) {
1014 if (Lexer.isNot(AsmToken::Comma))
Daniel Dunbarc238b582009-06-25 22:44:51 +00001015 return TokError("unexpected token in '.org' directive");
1016 Lexer.Lex();
1017
Daniel Dunbar475839e2009-06-29 20:37:27 +00001018 if (ParseAbsoluteExpression(FillExpr))
Daniel Dunbarc238b582009-06-25 22:44:51 +00001019 return true;
1020
Daniel Dunbar3f872332009-07-28 16:08:33 +00001021 if (Lexer.isNot(AsmToken::EndOfStatement))
Daniel Dunbarc238b582009-06-25 22:44:51 +00001022 return TokError("unexpected token in '.org' directive");
1023 }
1024
1025 Lexer.Lex();
Daniel Dunbarf4b830f2009-06-30 02:10:03 +00001026
1027 // FIXME: Only limited forms of relocatable expressions are accepted here, it
1028 // has to be relative to the current section.
1029 Out.EmitValueToOffset(Offset, FillExpr);
Daniel Dunbarc238b582009-06-25 22:44:51 +00001030
1031 return false;
1032}
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001033
1034/// ParseDirectiveAlign
1035/// ::= {.align, ...} expression [ , expression [ , expression ]]
1036bool AsmParser::ParseDirectiveAlign(bool IsPow2, unsigned ValueSize) {
1037 int64_t Alignment;
1038 if (ParseAbsoluteExpression(Alignment))
1039 return true;
1040
1041 SMLoc MaxBytesLoc;
1042 bool HasFillExpr = false;
1043 int64_t FillExpr = 0;
1044 int64_t MaxBytesToFill = 0;
Daniel Dunbar3f872332009-07-28 16:08:33 +00001045 if (Lexer.isNot(AsmToken::EndOfStatement)) {
1046 if (Lexer.isNot(AsmToken::Comma))
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001047 return TokError("unexpected token in directive");
1048 Lexer.Lex();
1049
1050 // The fill expression can be omitted while specifying a maximum number of
1051 // alignment bytes, e.g:
1052 // .align 3,,4
Daniel Dunbar3f872332009-07-28 16:08:33 +00001053 if (Lexer.isNot(AsmToken::Comma)) {
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001054 HasFillExpr = true;
1055 if (ParseAbsoluteExpression(FillExpr))
1056 return true;
1057 }
1058
Daniel Dunbar3f872332009-07-28 16:08:33 +00001059 if (Lexer.isNot(AsmToken::EndOfStatement)) {
1060 if (Lexer.isNot(AsmToken::Comma))
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001061 return TokError("unexpected token in directive");
1062 Lexer.Lex();
1063
1064 MaxBytesLoc = Lexer.getLoc();
1065 if (ParseAbsoluteExpression(MaxBytesToFill))
1066 return true;
1067
Daniel Dunbar3f872332009-07-28 16:08:33 +00001068 if (Lexer.isNot(AsmToken::EndOfStatement))
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001069 return TokError("unexpected token in directive");
1070 }
1071 }
1072
1073 Lexer.Lex();
1074
1075 if (!HasFillExpr) {
1076 // FIXME: Sometimes fill with nop.
1077 FillExpr = 0;
1078 }
1079
1080 // Compute alignment in bytes.
1081 if (IsPow2) {
1082 // FIXME: Diagnose overflow.
Chris Lattner39750252009-07-11 22:32:37 +00001083 Alignment = 1LL << Alignment;
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001084 }
1085
Daniel Dunbar0afb9f52009-08-21 23:01:53 +00001086 // Diagnose non-sensical max bytes to fill, which are treated as missing (this
1087 // matches 'as').
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001088 if (MaxBytesLoc.isValid()) {
1089 if (MaxBytesToFill < 1) {
Daniel Dunbar3fb76832009-06-30 00:49:23 +00001090 Warning(MaxBytesLoc, "alignment directive can never be satisfied in this "
Daniel Dunbar0afb9f52009-08-21 23:01:53 +00001091 "many bytes, ignoring maximum bytes expression");
1092 MaxBytesToFill = 0;
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001093 }
1094
1095 if (MaxBytesToFill >= Alignment) {
Daniel Dunbar3fb76832009-06-30 00:49:23 +00001096 Warning(MaxBytesLoc, "maximum bytes expression exceeds alignment and "
1097 "has no effect");
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001098 MaxBytesToFill = 0;
1099 }
1100 }
1101
1102 // FIXME: Target specific behavior about how the "extra" bytes are filled.
1103 Out.EmitValueToAlignment(Alignment, FillExpr, ValueSize, MaxBytesToFill);
1104
1105 return false;
1106}
1107
Daniel Dunbard7b267b2009-06-30 00:33:19 +00001108/// ParseDirectiveSymbolAttribute
1109/// ::= { ".globl", ".weak", ... } [ identifier ( , identifier )* ]
1110bool AsmParser::ParseDirectiveSymbolAttribute(MCStreamer::SymbolAttr Attr) {
Daniel Dunbar3f872332009-07-28 16:08:33 +00001111 if (Lexer.isNot(AsmToken::EndOfStatement)) {
Daniel Dunbard7b267b2009-06-30 00:33:19 +00001112 for (;;) {
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00001113 StringRef Name;
1114
1115 if (ParseIdentifier(Name))
Daniel Dunbard7b267b2009-06-30 00:33:19 +00001116 return TokError("expected identifier in directive");
1117
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00001118 MCSymbol *Sym = Ctx.GetOrCreateSymbol(Name);
Daniel Dunbard7b267b2009-06-30 00:33:19 +00001119
1120 // If this is use of an undefined symbol then mark it external.
1121 if (!Sym->getSection() && !Ctx.GetSymbolValue(Sym))
1122 Sym->setExternal(true);
1123
1124 Out.EmitSymbolAttribute(Sym, Attr);
1125
Daniel Dunbar3f872332009-07-28 16:08:33 +00001126 if (Lexer.is(AsmToken::EndOfStatement))
Daniel Dunbard7b267b2009-06-30 00:33:19 +00001127 break;
1128
Daniel Dunbar3f872332009-07-28 16:08:33 +00001129 if (Lexer.isNot(AsmToken::Comma))
Daniel Dunbard7b267b2009-06-30 00:33:19 +00001130 return TokError("unexpected token in directive");
1131 Lexer.Lex();
1132 }
1133 }
1134
1135 Lexer.Lex();
1136 return false;
1137}
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001138
Kevin Enderby95cf30c2009-07-14 18:17:10 +00001139/// ParseDirectiveDarwinSymbolDesc
1140/// ::= .desc identifier , expression
1141bool AsmParser::ParseDirectiveDarwinSymbolDesc() {
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00001142 StringRef Name;
1143 if (ParseIdentifier(Name))
Kevin Enderby95cf30c2009-07-14 18:17:10 +00001144 return TokError("expected identifier in directive");
1145
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00001146 // Handle the identifier as the key symbol.
1147 MCSymbol *Sym = Ctx.GetOrCreateSymbol(Name);
Kevin Enderby95cf30c2009-07-14 18:17:10 +00001148
Daniel Dunbar3f872332009-07-28 16:08:33 +00001149 if (Lexer.isNot(AsmToken::Comma))
Kevin Enderby95cf30c2009-07-14 18:17:10 +00001150 return TokError("unexpected token in '.desc' directive");
1151 Lexer.Lex();
1152
1153 SMLoc DescLoc = Lexer.getLoc();
1154 int64_t DescValue;
1155 if (ParseAbsoluteExpression(DescValue))
1156 return true;
1157
Daniel Dunbar3f872332009-07-28 16:08:33 +00001158 if (Lexer.isNot(AsmToken::EndOfStatement))
Kevin Enderby95cf30c2009-07-14 18:17:10 +00001159 return TokError("unexpected token in '.desc' directive");
1160
1161 Lexer.Lex();
1162
1163 // Set the n_desc field of this Symbol to this DescValue
1164 Out.EmitSymbolDesc(Sym, DescValue);
1165
1166 return false;
1167}
1168
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001169/// ParseDirectiveComm
Chris Lattner1fc3d752009-07-09 17:25:12 +00001170/// ::= ( .comm | .lcomm ) identifier , size_expression [ , align_expression ]
1171bool AsmParser::ParseDirectiveComm(bool IsLocal) {
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00001172 SMLoc IDLoc = Lexer.getLoc();
1173 StringRef Name;
1174 if (ParseIdentifier(Name))
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001175 return TokError("expected identifier in directive");
1176
Daniel Dunbar76c4d762009-07-31 21:55:09 +00001177 // Handle the identifier as the key symbol.
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00001178 MCSymbol *Sym = Ctx.GetOrCreateSymbol(Name);
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001179
Daniel Dunbar3f872332009-07-28 16:08:33 +00001180 if (Lexer.isNot(AsmToken::Comma))
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001181 return TokError("unexpected token in directive");
1182 Lexer.Lex();
1183
1184 int64_t Size;
1185 SMLoc SizeLoc = Lexer.getLoc();
1186 if (ParseAbsoluteExpression(Size))
1187 return true;
1188
1189 int64_t Pow2Alignment = 0;
1190 SMLoc Pow2AlignmentLoc;
Daniel Dunbar3f872332009-07-28 16:08:33 +00001191 if (Lexer.is(AsmToken::Comma)) {
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001192 Lexer.Lex();
1193 Pow2AlignmentLoc = Lexer.getLoc();
1194 if (ParseAbsoluteExpression(Pow2Alignment))
1195 return true;
1196 }
1197
Daniel Dunbar3f872332009-07-28 16:08:33 +00001198 if (Lexer.isNot(AsmToken::EndOfStatement))
Chris Lattner1fc3d752009-07-09 17:25:12 +00001199 return TokError("unexpected token in '.comm' or '.lcomm' directive");
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001200
1201 Lexer.Lex();
1202
Chris Lattner1fc3d752009-07-09 17:25:12 +00001203 // NOTE: a size of zero for a .comm should create a undefined symbol
1204 // but a size of .lcomm creates a bss symbol of size zero.
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001205 if (Size < 0)
Chris Lattner1fc3d752009-07-09 17:25:12 +00001206 return Error(SizeLoc, "invalid '.comm' or '.lcomm' directive size, can't "
1207 "be less than zero");
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001208
1209 // NOTE: The alignment in the directive is a power of 2 value, the assember
1210 // may internally end up wanting an alignment in bytes.
1211 // FIXME: Diagnose overflow.
1212 if (Pow2Alignment < 0)
Chris Lattner1fc3d752009-07-09 17:25:12 +00001213 return Error(Pow2AlignmentLoc, "invalid '.comm' or '.lcomm' directive "
1214 "alignment, can't be less than zero");
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001215
1216 // TODO: Symbol must be undefined or it is a error to re-defined the symbol
1217 if (Sym->getSection() || Ctx.GetSymbolValue(Sym))
1218 return Error(IDLoc, "invalid symbol redefinition");
1219
Chris Lattner1fc3d752009-07-09 17:25:12 +00001220 // Create the Symbol as a common or local common with Size and Pow2Alignment
1221 Out.EmitCommonSymbol(Sym, Size, Pow2Alignment, IsLocal);
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001222
1223 return false;
1224}
Chris Lattner9be3fee2009-07-10 22:20:30 +00001225
1226/// ParseDirectiveDarwinZerofill
1227/// ::= .zerofill segname , sectname [, identifier , size_expression [
1228/// , align_expression ]]
1229bool AsmParser::ParseDirectiveDarwinZerofill() {
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00001230 // FIXME: Handle quoted names here.
1231
Daniel Dunbar3f872332009-07-28 16:08:33 +00001232 if (Lexer.isNot(AsmToken::Identifier))
Chris Lattner9be3fee2009-07-10 22:20:30 +00001233 return TokError("expected segment name after '.zerofill' directive");
Chris Lattnerff4bc462009-08-10 01:39:42 +00001234 StringRef Segment = Lexer.getTok().getString();
Chris Lattner9be3fee2009-07-10 22:20:30 +00001235 Lexer.Lex();
1236
Daniel Dunbar3f872332009-07-28 16:08:33 +00001237 if (Lexer.isNot(AsmToken::Comma))
Chris Lattner9be3fee2009-07-10 22:20:30 +00001238 return TokError("unexpected token in directive");
Chris Lattner9be3fee2009-07-10 22:20:30 +00001239 Lexer.Lex();
1240
Daniel Dunbar3f872332009-07-28 16:08:33 +00001241 if (Lexer.isNot(AsmToken::Identifier))
Chris Lattner9be3fee2009-07-10 22:20:30 +00001242 return TokError("expected section name after comma in '.zerofill' "
1243 "directive");
Chris Lattnerff4bc462009-08-10 01:39:42 +00001244 StringRef Section = Lexer.getTok().getString();
Chris Lattner9be3fee2009-07-10 22:20:30 +00001245 Lexer.Lex();
1246
Chris Lattner9be3fee2009-07-10 22:20:30 +00001247 // If this is the end of the line all that was wanted was to create the
1248 // the section but with no symbol.
Daniel Dunbar3f872332009-07-28 16:08:33 +00001249 if (Lexer.is(AsmToken::EndOfStatement)) {
Chris Lattnerff4bc462009-08-10 01:39:42 +00001250 // FIXME: CACHE THIS.
1251 MCSection *S = 0; //Ctx.GetSection(Section);
Chris Lattner6bdd74c2009-07-31 18:27:48 +00001252 if (S == 0)
Chris Lattnerff4bc462009-08-10 01:39:42 +00001253 S = MCSectionMachO::Create(Segment, Section,
1254 MCSectionMachO::S_ZEROFILL, 0,
1255 SectionKind(), Ctx);
Chris Lattner6bdd74c2009-07-31 18:27:48 +00001256
Chris Lattner9be3fee2009-07-10 22:20:30 +00001257 // Create the zerofill section but no symbol
Chris Lattner6bdd74c2009-07-31 18:27:48 +00001258 Out.EmitZerofill(S);
Chris Lattner9be3fee2009-07-10 22:20:30 +00001259 return false;
1260 }
1261
Daniel Dunbar3f872332009-07-28 16:08:33 +00001262 if (Lexer.isNot(AsmToken::Comma))
Chris Lattner9be3fee2009-07-10 22:20:30 +00001263 return TokError("unexpected token in directive");
1264 Lexer.Lex();
1265
Daniel Dunbar3f872332009-07-28 16:08:33 +00001266 if (Lexer.isNot(AsmToken::Identifier))
Chris Lattner9be3fee2009-07-10 22:20:30 +00001267 return TokError("expected identifier in directive");
1268
1269 // handle the identifier as the key symbol.
1270 SMLoc IDLoc = Lexer.getLoc();
Daniel Dunbar419aded2009-07-28 16:38:40 +00001271 MCSymbol *Sym = Ctx.GetOrCreateSymbol(Lexer.getTok().getString());
Chris Lattner9be3fee2009-07-10 22:20:30 +00001272 Lexer.Lex();
1273
Daniel Dunbar3f872332009-07-28 16:08:33 +00001274 if (Lexer.isNot(AsmToken::Comma))
Chris Lattner9be3fee2009-07-10 22:20:30 +00001275 return TokError("unexpected token in directive");
1276 Lexer.Lex();
1277
1278 int64_t Size;
1279 SMLoc SizeLoc = Lexer.getLoc();
1280 if (ParseAbsoluteExpression(Size))
1281 return true;
1282
1283 int64_t Pow2Alignment = 0;
1284 SMLoc Pow2AlignmentLoc;
Daniel Dunbar3f872332009-07-28 16:08:33 +00001285 if (Lexer.is(AsmToken::Comma)) {
Chris Lattner9be3fee2009-07-10 22:20:30 +00001286 Lexer.Lex();
1287 Pow2AlignmentLoc = Lexer.getLoc();
1288 if (ParseAbsoluteExpression(Pow2Alignment))
1289 return true;
1290 }
1291
Daniel Dunbar3f872332009-07-28 16:08:33 +00001292 if (Lexer.isNot(AsmToken::EndOfStatement))
Chris Lattner9be3fee2009-07-10 22:20:30 +00001293 return TokError("unexpected token in '.zerofill' directive");
1294
1295 Lexer.Lex();
1296
1297 if (Size < 0)
1298 return Error(SizeLoc, "invalid '.zerofill' directive size, can't be less "
1299 "than zero");
1300
1301 // NOTE: The alignment in the directive is a power of 2 value, the assember
1302 // may internally end up wanting an alignment in bytes.
1303 // FIXME: Diagnose overflow.
1304 if (Pow2Alignment < 0)
1305 return Error(Pow2AlignmentLoc, "invalid '.zerofill' directive alignment, "
1306 "can't be less than zero");
1307
1308 // TODO: Symbol must be undefined or it is a error to re-defined the symbol
1309 if (Sym->getSection() || Ctx.GetSymbolValue(Sym))
1310 return Error(IDLoc, "invalid symbol redefinition");
1311
Chris Lattner6bdd74c2009-07-31 18:27:48 +00001312 // FIXME: Arch specific.
Chris Lattnerff4bc462009-08-10 01:39:42 +00001313 // FIXME: CACHE.
1314 MCSection *S = 0; //Ctx.GetSection(Section);
Chris Lattner6bdd74c2009-07-31 18:27:48 +00001315 if (S == 0)
Chris Lattnerff4bc462009-08-10 01:39:42 +00001316 S = MCSectionMachO::Create(Segment, Section,
1317 MCSectionMachO::S_ZEROFILL, 0,
1318 SectionKind(), Ctx);
Chris Lattner6bdd74c2009-07-31 18:27:48 +00001319
Chris Lattner9be3fee2009-07-10 22:20:30 +00001320 // Create the zerofill Symbol with Size and Pow2Alignment
Chris Lattner6bdd74c2009-07-31 18:27:48 +00001321 Out.EmitZerofill(S, Sym, Size, Pow2Alignment);
Chris Lattner9be3fee2009-07-10 22:20:30 +00001322
1323 return false;
1324}
Kevin Enderbya5c78322009-07-13 21:03:15 +00001325
1326/// ParseDirectiveDarwinSubsectionsViaSymbols
1327/// ::= .subsections_via_symbols
1328bool AsmParser::ParseDirectiveDarwinSubsectionsViaSymbols() {
Daniel Dunbar3f872332009-07-28 16:08:33 +00001329 if (Lexer.isNot(AsmToken::EndOfStatement))
Kevin Enderbya5c78322009-07-13 21:03:15 +00001330 return TokError("unexpected token in '.subsections_via_symbols' directive");
1331
1332 Lexer.Lex();
1333
Kevin Enderbyf96db462009-07-16 17:56:39 +00001334 Out.EmitAssemblerFlag(MCStreamer::SubsectionsViaSymbols);
Kevin Enderbya5c78322009-07-13 21:03:15 +00001335
1336 return false;
1337}
Kevin Enderby5f1f0b82009-07-13 23:15:14 +00001338
1339/// ParseDirectiveAbort
1340/// ::= .abort [ "abort_string" ]
1341bool AsmParser::ParseDirectiveAbort() {
Daniel Dunbarf9507ff2009-07-27 23:20:52 +00001342 // FIXME: Use loc from directive.
1343 SMLoc Loc = Lexer.getLoc();
1344
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001345 StringRef Str = "";
Daniel Dunbar3f872332009-07-28 16:08:33 +00001346 if (Lexer.isNot(AsmToken::EndOfStatement)) {
1347 if (Lexer.isNot(AsmToken::String))
Kevin Enderby5f1f0b82009-07-13 23:15:14 +00001348 return TokError("expected string in '.abort' directive");
1349
Daniel Dunbar419aded2009-07-28 16:38:40 +00001350 Str = Lexer.getTok().getString();
Kevin Enderby5f1f0b82009-07-13 23:15:14 +00001351
1352 Lexer.Lex();
1353 }
1354
Daniel Dunbar3f872332009-07-28 16:08:33 +00001355 if (Lexer.isNot(AsmToken::EndOfStatement))
Kevin Enderby5f1f0b82009-07-13 23:15:14 +00001356 return TokError("unexpected token in '.abort' directive");
1357
1358 Lexer.Lex();
1359
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001360 // FIXME: Handle here.
Daniel Dunbarf9507ff2009-07-27 23:20:52 +00001361 if (Str.empty())
1362 Error(Loc, ".abort detected. Assembly stopping.");
1363 else
1364 Error(Loc, ".abort '" + Str + "' detected. Assembly stopping.");
Kevin Enderby5f1f0b82009-07-13 23:15:14 +00001365
1366 return false;
1367}
Kevin Enderby71148242009-07-14 21:35:03 +00001368
1369/// ParseDirectiveLsym
1370/// ::= .lsym identifier , expression
1371bool AsmParser::ParseDirectiveDarwinLsym() {
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00001372 StringRef Name;
1373 if (ParseIdentifier(Name))
Kevin Enderby71148242009-07-14 21:35:03 +00001374 return TokError("expected identifier in directive");
1375
Daniel Dunbar76c4d762009-07-31 21:55:09 +00001376 // Handle the identifier as the key symbol.
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00001377 MCSymbol *Sym = Ctx.GetOrCreateSymbol(Name);
Kevin Enderby71148242009-07-14 21:35:03 +00001378
Daniel Dunbar3f872332009-07-28 16:08:33 +00001379 if (Lexer.isNot(AsmToken::Comma))
Kevin Enderby71148242009-07-14 21:35:03 +00001380 return TokError("unexpected token in '.lsym' directive");
1381 Lexer.Lex();
1382
1383 MCValue Expr;
1384 if (ParseRelocatableExpression(Expr))
1385 return true;
1386
Daniel Dunbar3f872332009-07-28 16:08:33 +00001387 if (Lexer.isNot(AsmToken::EndOfStatement))
Kevin Enderby71148242009-07-14 21:35:03 +00001388 return TokError("unexpected token in '.lsym' directive");
1389
1390 Lexer.Lex();
1391
1392 // Create the Sym with the value of the Expr
1393 Out.EmitLocalSymbol(Sym, Expr);
1394
1395 return false;
1396}
Kevin Enderby1f049b22009-07-14 23:21:55 +00001397
1398/// ParseDirectiveInclude
1399/// ::= .include "filename"
1400bool AsmParser::ParseDirectiveInclude() {
Daniel Dunbar3f872332009-07-28 16:08:33 +00001401 if (Lexer.isNot(AsmToken::String))
Kevin Enderby1f049b22009-07-14 23:21:55 +00001402 return TokError("expected string in '.include' directive");
1403
Daniel Dunbar419aded2009-07-28 16:38:40 +00001404 std::string Filename = Lexer.getTok().getString();
Chris Lattner8e25e2d2009-07-16 06:14:39 +00001405 SMLoc IncludeLoc = Lexer.getLoc();
Kevin Enderby1f049b22009-07-14 23:21:55 +00001406 Lexer.Lex();
1407
Daniel Dunbar3f872332009-07-28 16:08:33 +00001408 if (Lexer.isNot(AsmToken::EndOfStatement))
Kevin Enderby1f049b22009-07-14 23:21:55 +00001409 return TokError("unexpected token in '.include' directive");
1410
Chris Lattner8e25e2d2009-07-16 06:14:39 +00001411 // Strip the quotes.
1412 Filename = Filename.substr(1, Filename.size()-2);
1413
1414 // Attempt to switch the lexer to the included file before consuming the end
1415 // of statement to avoid losing it when we switch.
1416 if (Lexer.EnterIncludeFile(Filename)) {
1417 Lexer.PrintMessage(IncludeLoc,
1418 "Could not find include file '" + Filename + "'",
1419 "error");
1420 return true;
1421 }
Kevin Enderby1f049b22009-07-14 23:21:55 +00001422
1423 return false;
1424}
Kevin Enderby6e68cd92009-07-15 15:30:11 +00001425
1426/// ParseDirectiveDarwinDumpOrLoad
1427/// ::= ( .dump | .load ) "filename"
Kevin Enderby5026ae42009-07-20 20:25:37 +00001428bool AsmParser::ParseDirectiveDarwinDumpOrLoad(SMLoc IDLoc, bool IsDump) {
Daniel Dunbar3f872332009-07-28 16:08:33 +00001429 if (Lexer.isNot(AsmToken::String))
Kevin Enderby6e68cd92009-07-15 15:30:11 +00001430 return TokError("expected string in '.dump' or '.load' directive");
1431
Kevin Enderby6e68cd92009-07-15 15:30:11 +00001432 Lexer.Lex();
1433
Daniel Dunbar3f872332009-07-28 16:08:33 +00001434 if (Lexer.isNot(AsmToken::EndOfStatement))
Kevin Enderby6e68cd92009-07-15 15:30:11 +00001435 return TokError("unexpected token in '.dump' or '.load' directive");
1436
1437 Lexer.Lex();
1438
Kevin Enderby5026ae42009-07-20 20:25:37 +00001439 // FIXME: If/when .dump and .load are implemented they will be done in the
1440 // the assembly parser and not have any need for an MCStreamer API.
Kevin Enderby6e68cd92009-07-15 15:30:11 +00001441 if (IsDump)
Kevin Enderby5026ae42009-07-20 20:25:37 +00001442 Warning(IDLoc, "ignoring directive .dump for now");
Kevin Enderby6e68cd92009-07-15 15:30:11 +00001443 else
Kevin Enderby5026ae42009-07-20 20:25:37 +00001444 Warning(IDLoc, "ignoring directive .load for now");
Kevin Enderby6e68cd92009-07-15 15:30:11 +00001445
1446 return false;
1447}
Kevin Enderbyc114ed72009-08-07 22:46:00 +00001448
1449/// ParseDirectiveIf
1450/// ::= .if expression
1451bool AsmParser::ParseDirectiveIf(SMLoc DirectiveLoc) {
1452 // Consume the identifier that was the .if directive
1453 Lexer.Lex();
1454
1455 TheCondStack.push_back(TheCondState);
1456 TheCondState.TheCond = AsmCond::IfCond;
1457 if(TheCondState.Ignore) {
1458 EatToEndOfStatement();
1459 }
1460 else {
1461 int64_t ExprValue;
1462 if (ParseAbsoluteExpression(ExprValue))
1463 return true;
1464
1465 if (Lexer.isNot(AsmToken::EndOfStatement))
1466 return TokError("unexpected token in '.if' directive");
1467
1468 Lexer.Lex();
1469
1470 TheCondState.CondMet = ExprValue;
1471 TheCondState.Ignore = !TheCondState.CondMet;
1472 }
1473
1474 return false;
1475}
1476
1477/// ParseDirectiveElseIf
1478/// ::= .elseif expression
1479bool AsmParser::ParseDirectiveElseIf(SMLoc DirectiveLoc) {
1480 if (TheCondState.TheCond != AsmCond::IfCond &&
1481 TheCondState.TheCond != AsmCond::ElseIfCond)
1482 Error(DirectiveLoc, "Encountered a .elseif that doesn't follow a .if or "
1483 " an .elseif");
1484 TheCondState.TheCond = AsmCond::ElseIfCond;
1485
1486 // Consume the identifier that was the .elseif directive
1487 Lexer.Lex();
1488
1489 bool LastIgnoreState = false;
1490 if (!TheCondStack.empty())
1491 LastIgnoreState = TheCondStack.back().Ignore;
1492 if (LastIgnoreState || TheCondState.CondMet) {
1493 TheCondState.Ignore = true;
1494 EatToEndOfStatement();
1495 }
1496 else {
1497 int64_t ExprValue;
1498 if (ParseAbsoluteExpression(ExprValue))
1499 return true;
1500
1501 if (Lexer.isNot(AsmToken::EndOfStatement))
1502 return TokError("unexpected token in '.elseif' directive");
1503
1504 Lexer.Lex();
1505 TheCondState.CondMet = ExprValue;
1506 TheCondState.Ignore = !TheCondState.CondMet;
1507 }
1508
1509 return false;
1510}
1511
1512/// ParseDirectiveElse
1513/// ::= .else
1514bool AsmParser::ParseDirectiveElse(SMLoc DirectiveLoc) {
1515 // Consume the identifier that was the .else directive
1516 Lexer.Lex();
1517
1518 if (Lexer.isNot(AsmToken::EndOfStatement))
1519 return TokError("unexpected token in '.else' directive");
1520
1521 Lexer.Lex();
1522
1523 if (TheCondState.TheCond != AsmCond::IfCond &&
1524 TheCondState.TheCond != AsmCond::ElseIfCond)
1525 Error(DirectiveLoc, "Encountered a .else that doesn't follow a .if or an "
1526 ".elseif");
1527 TheCondState.TheCond = AsmCond::ElseCond;
1528 bool LastIgnoreState = false;
1529 if (!TheCondStack.empty())
1530 LastIgnoreState = TheCondStack.back().Ignore;
1531 if (LastIgnoreState || TheCondState.CondMet)
1532 TheCondState.Ignore = true;
1533 else
1534 TheCondState.Ignore = false;
1535
1536 return false;
1537}
1538
1539/// ParseDirectiveEndIf
1540/// ::= .endif
1541bool AsmParser::ParseDirectiveEndIf(SMLoc DirectiveLoc) {
1542 // Consume the identifier that was the .endif directive
1543 Lexer.Lex();
1544
1545 if (Lexer.isNot(AsmToken::EndOfStatement))
1546 return TokError("unexpected token in '.endif' directive");
1547
1548 Lexer.Lex();
1549
1550 if ((TheCondState.TheCond == AsmCond::NoCond) ||
1551 TheCondStack.empty())
1552 Error(DirectiveLoc, "Encountered a .endif that doesn't follow a .if or "
1553 ".else");
1554 if (!TheCondStack.empty()) {
1555 TheCondState = TheCondStack.back();
1556 TheCondStack.pop_back();
1557 }
1558
1559 return false;
1560}
Daniel Dunbard0c14d62009-08-11 04:24:50 +00001561
1562/// ParseDirectiveFile
1563/// ::= .file [number] string
1564bool AsmParser::ParseDirectiveFile(SMLoc DirectiveLoc) {
1565 // FIXME: I'm not sure what this is.
1566 int64_t FileNumber = -1;
1567 if (Lexer.is(AsmToken::Integer)) {
1568 FileNumber = Lexer.getTok().getIntVal();
1569 Lexer.Lex();
1570
1571 if (FileNumber < 1)
1572 return TokError("file number less than one");
1573 }
1574
1575 if (Lexer.isNot(AsmToken::String))
1576 return TokError("unexpected token in '.file' directive");
1577
1578 StringRef FileName = Lexer.getTok().getString();
1579 Lexer.Lex();
1580
1581 if (Lexer.isNot(AsmToken::EndOfStatement))
1582 return TokError("unexpected token in '.file' directive");
1583
1584 // FIXME: Do something with the .file.
1585
1586 return false;
1587}
1588
1589/// ParseDirectiveLine
1590/// ::= .line [number]
1591bool AsmParser::ParseDirectiveLine(SMLoc DirectiveLoc) {
1592 if (Lexer.isNot(AsmToken::EndOfStatement)) {
1593 if (Lexer.isNot(AsmToken::Integer))
1594 return TokError("unexpected token in '.line' directive");
1595
1596 int64_t LineNumber = Lexer.getTok().getIntVal();
1597 (void) LineNumber;
1598 Lexer.Lex();
1599
1600 // FIXME: Do something with the .line.
1601 }
1602
1603 if (Lexer.isNot(AsmToken::EndOfStatement))
1604 return TokError("unexpected token in '.file' directive");
1605
1606 return false;
1607}
1608
1609
1610/// ParseDirectiveLoc
1611/// ::= .loc number [number [number]]
1612bool AsmParser::ParseDirectiveLoc(SMLoc DirectiveLoc) {
1613 if (Lexer.isNot(AsmToken::Integer))
1614 return TokError("unexpected token in '.loc' directive");
1615
1616 // FIXME: What are these fields?
1617 int64_t FileNumber = Lexer.getTok().getIntVal();
1618 (void) FileNumber;
1619 // FIXME: Validate file.
1620
1621 Lexer.Lex();
1622 if (Lexer.isNot(AsmToken::EndOfStatement)) {
1623 if (Lexer.isNot(AsmToken::Integer))
1624 return TokError("unexpected token in '.loc' directive");
1625
1626 int64_t Param2 = Lexer.getTok().getIntVal();
1627 (void) Param2;
1628 Lexer.Lex();
1629
1630 if (Lexer.isNot(AsmToken::EndOfStatement)) {
1631 if (Lexer.isNot(AsmToken::Integer))
1632 return TokError("unexpected token in '.loc' directive");
1633
1634 int64_t Param3 = Lexer.getTok().getIntVal();
1635 (void) Param3;
1636 Lexer.Lex();
1637
1638 // FIXME: Do something with the .loc.
1639 }
1640 }
1641
1642 if (Lexer.isNot(AsmToken::EndOfStatement))
1643 return TokError("unexpected token in '.file' directive");
1644
1645 return false;
1646}
1647