blob: 24c830127ac368f89146e94217a24f6ec2bc3902 [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 Res = new AsmSymbolRefExpr(Sym);
Chris Lattnerc4193832009-06-22 05:51:26 +0000154 Lexer.Lex(); // Eat identifier.
155 return false;
Daniel Dunbardce0f3c2009-06-29 23:43:14 +0000156 }
Daniel Dunbar3f872332009-07-28 16:08:33 +0000157 case AsmToken::Integer:
Daniel Dunbar419aded2009-07-28 16:38:40 +0000158 Res = new AsmConstantExpr(Lexer.getTok().getIntVal());
Daniel Dunbar76c4d762009-07-31 21:55:09 +0000159 Lexer.Lex(); // Eat token.
Chris Lattnerc4193832009-06-22 05:51:26 +0000160 return false;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000161 case AsmToken::LParen:
Chris Lattner74ec1a32009-06-22 06:32:03 +0000162 Lexer.Lex(); // Eat the '('.
163 return ParseParenExpr(Res);
Daniel Dunbar3f872332009-07-28 16:08:33 +0000164 case AsmToken::Minus:
Chris Lattner74ec1a32009-06-22 06:32:03 +0000165 Lexer.Lex(); // Eat the operator.
Daniel Dunbar475839e2009-06-29 20:37:27 +0000166 if (ParsePrimaryExpr(Res))
167 return true;
168 Res = new AsmUnaryExpr(AsmUnaryExpr::Minus, Res);
169 return false;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000170 case AsmToken::Plus:
Daniel Dunbar475839e2009-06-29 20:37:27 +0000171 Lexer.Lex(); // Eat the operator.
172 if (ParsePrimaryExpr(Res))
173 return true;
174 Res = new AsmUnaryExpr(AsmUnaryExpr::Plus, Res);
175 return false;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000176 case AsmToken::Tilde:
Daniel Dunbar475839e2009-06-29 20:37:27 +0000177 Lexer.Lex(); // Eat the operator.
178 if (ParsePrimaryExpr(Res))
179 return true;
180 Res = new AsmUnaryExpr(AsmUnaryExpr::Not, Res);
181 return false;
Chris Lattnerc4193832009-06-22 05:51:26 +0000182 }
183}
Chris Lattner74ec1a32009-06-22 06:32:03 +0000184
185/// ParseExpression - Parse an expression and return it.
186///
187/// expr ::= expr +,- expr -> lowest.
188/// expr ::= expr |,^,&,! expr -> middle.
189/// expr ::= expr *,/,%,<<,>> expr -> highest.
190/// expr ::= primaryexpr
191///
Daniel Dunbar475839e2009-06-29 20:37:27 +0000192bool AsmParser::ParseExpression(AsmExpr *&Res) {
193 Res = 0;
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000194 return ParsePrimaryExpr(Res) ||
195 ParseBinOpRHS(1, Res);
Chris Lattner74ec1a32009-06-22 06:32:03 +0000196}
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000197
Daniel Dunbar475839e2009-06-29 20:37:27 +0000198bool AsmParser::ParseAbsoluteExpression(int64_t &Res) {
199 AsmExpr *Expr;
200
Daniel Dunbarf4b830f2009-06-30 02:10:03 +0000201 SMLoc StartLoc = Lexer.getLoc();
Daniel Dunbar475839e2009-06-29 20:37:27 +0000202 if (ParseExpression(Expr))
203 return true;
204
205 if (!Expr->EvaluateAsAbsolute(Ctx, Res))
Daniel Dunbarf4b830f2009-06-30 02:10:03 +0000206 return Error(StartLoc, "expected absolute expression");
Daniel Dunbar475839e2009-06-29 20:37:27 +0000207
208 return false;
209}
210
Daniel Dunbar15d17072009-06-30 01:49:52 +0000211bool AsmParser::ParseRelocatableExpression(MCValue &Res) {
212 AsmExpr *Expr;
213
Daniel Dunbarf4b830f2009-06-30 02:10:03 +0000214 SMLoc StartLoc = Lexer.getLoc();
Daniel Dunbar15d17072009-06-30 01:49:52 +0000215 if (ParseExpression(Expr))
216 return true;
217
218 if (!Expr->EvaluateAsRelocatable(Ctx, Res))
Daniel Dunbarf4b830f2009-06-30 02:10:03 +0000219 return Error(StartLoc, "expected relocatable expression");
Daniel Dunbar15d17072009-06-30 01:49:52 +0000220
221 return false;
222}
223
Daniel Dunbar2c3f00c2009-07-02 02:09:07 +0000224bool AsmParser::ParseParenRelocatableExpression(MCValue &Res) {
225 AsmExpr *Expr;
226
227 SMLoc StartLoc = Lexer.getLoc();
228 if (ParseParenExpr(Expr))
229 return true;
230
231 if (!Expr->EvaluateAsRelocatable(Ctx, Res))
232 return Error(StartLoc, "expected relocatable expression");
233
234 return false;
235}
236
Daniel Dunbar3f872332009-07-28 16:08:33 +0000237static unsigned getBinOpPrecedence(AsmToken::TokenKind K,
Daniel Dunbar475839e2009-06-29 20:37:27 +0000238 AsmBinaryExpr::Opcode &Kind) {
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000239 switch (K) {
240 default: return 0; // not a binop.
Daniel Dunbar475839e2009-06-29 20:37:27 +0000241
242 // Lowest Precedence: &&, ||
Daniel Dunbar3f872332009-07-28 16:08:33 +0000243 case AsmToken::AmpAmp:
Daniel Dunbar475839e2009-06-29 20:37:27 +0000244 Kind = AsmBinaryExpr::LAnd;
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000245 return 1;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000246 case AsmToken::PipePipe:
Daniel Dunbar475839e2009-06-29 20:37:27 +0000247 Kind = AsmBinaryExpr::LOr;
248 return 1;
249
250 // Low Precedence: +, -, ==, !=, <>, <, <=, >, >=
Daniel Dunbar3f872332009-07-28 16:08:33 +0000251 case AsmToken::Plus:
Daniel Dunbar475839e2009-06-29 20:37:27 +0000252 Kind = AsmBinaryExpr::Add;
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000253 return 2;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000254 case AsmToken::Minus:
Daniel Dunbar475839e2009-06-29 20:37:27 +0000255 Kind = AsmBinaryExpr::Sub;
256 return 2;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000257 case AsmToken::EqualEqual:
Daniel Dunbar475839e2009-06-29 20:37:27 +0000258 Kind = AsmBinaryExpr::EQ;
259 return 2;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000260 case AsmToken::ExclaimEqual:
261 case AsmToken::LessGreater:
Daniel Dunbar475839e2009-06-29 20:37:27 +0000262 Kind = AsmBinaryExpr::NE;
263 return 2;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000264 case AsmToken::Less:
Daniel Dunbar475839e2009-06-29 20:37:27 +0000265 Kind = AsmBinaryExpr::LT;
266 return 2;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000267 case AsmToken::LessEqual:
Daniel Dunbar475839e2009-06-29 20:37:27 +0000268 Kind = AsmBinaryExpr::LTE;
269 return 2;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000270 case AsmToken::Greater:
Daniel Dunbar475839e2009-06-29 20:37:27 +0000271 Kind = AsmBinaryExpr::GT;
272 return 2;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000273 case AsmToken::GreaterEqual:
Daniel Dunbar475839e2009-06-29 20:37:27 +0000274 Kind = AsmBinaryExpr::GTE;
275 return 2;
276
277 // Intermediate Precedence: |, &, ^
278 //
279 // FIXME: gas seems to support '!' as an infix operator?
Daniel Dunbar3f872332009-07-28 16:08:33 +0000280 case AsmToken::Pipe:
Daniel Dunbar475839e2009-06-29 20:37:27 +0000281 Kind = AsmBinaryExpr::Or;
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000282 return 3;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000283 case AsmToken::Caret:
Daniel Dunbar475839e2009-06-29 20:37:27 +0000284 Kind = AsmBinaryExpr::Xor;
285 return 3;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000286 case AsmToken::Amp:
Daniel Dunbar475839e2009-06-29 20:37:27 +0000287 Kind = AsmBinaryExpr::And;
288 return 3;
289
290 // Highest Precedence: *, /, %, <<, >>
Daniel Dunbar3f872332009-07-28 16:08:33 +0000291 case AsmToken::Star:
Daniel Dunbar475839e2009-06-29 20:37:27 +0000292 Kind = AsmBinaryExpr::Mul;
293 return 4;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000294 case AsmToken::Slash:
Daniel Dunbar475839e2009-06-29 20:37:27 +0000295 Kind = AsmBinaryExpr::Div;
296 return 4;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000297 case AsmToken::Percent:
Daniel Dunbar475839e2009-06-29 20:37:27 +0000298 Kind = AsmBinaryExpr::Mod;
299 return 4;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000300 case AsmToken::LessLess:
Daniel Dunbar475839e2009-06-29 20:37:27 +0000301 Kind = AsmBinaryExpr::Shl;
302 return 4;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000303 case AsmToken::GreaterGreater:
Daniel Dunbar475839e2009-06-29 20:37:27 +0000304 Kind = AsmBinaryExpr::Shr;
305 return 4;
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000306 }
307}
308
309
310/// ParseBinOpRHS - Parse all binary operators with precedence >= 'Precedence'.
311/// Res contains the LHS of the expression on input.
Daniel Dunbar475839e2009-06-29 20:37:27 +0000312bool AsmParser::ParseBinOpRHS(unsigned Precedence, AsmExpr *&Res) {
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000313 while (1) {
Daniel Dunbar51330632009-06-29 21:14:21 +0000314 AsmBinaryExpr::Opcode Kind = AsmBinaryExpr::Add;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000315 unsigned TokPrec = getBinOpPrecedence(Lexer.getKind(), Kind);
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000316
317 // If the next token is lower precedence than we are allowed to eat, return
318 // successfully with what we ate already.
319 if (TokPrec < Precedence)
320 return false;
321
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000322 Lexer.Lex();
323
324 // Eat the next primary expression.
Daniel Dunbar475839e2009-06-29 20:37:27 +0000325 AsmExpr *RHS;
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000326 if (ParsePrimaryExpr(RHS)) return true;
327
328 // If BinOp binds less tightly with RHS than the operator after RHS, let
329 // the pending operator take RHS as its LHS.
Daniel Dunbar475839e2009-06-29 20:37:27 +0000330 AsmBinaryExpr::Opcode Dummy;
331 unsigned NextTokPrec = getBinOpPrecedence(Lexer.getKind(), Dummy);
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000332 if (TokPrec < NextTokPrec) {
333 if (ParseBinOpRHS(Precedence+1, RHS)) return true;
334 }
335
Daniel Dunbar475839e2009-06-29 20:37:27 +0000336 // Merge LHS and RHS according to operator.
337 Res = new AsmBinaryExpr(Kind, Res, RHS);
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000338 }
339}
340
Chris Lattnerc4193832009-06-22 05:51:26 +0000341
342
343
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000344/// ParseStatement:
345/// ::= EndOfStatement
Chris Lattner2cf5f142009-06-22 01:29:09 +0000346/// ::= Label* Directive ...Operands... EndOfStatement
347/// ::= Label* Identifier OperandList* EndOfStatement
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000348bool AsmParser::ParseStatement() {
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +0000349 if (Lexer.is(AsmToken::EndOfStatement)) {
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000350 Lexer.Lex();
351 return false;
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000352 }
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +0000353
354 // Statements always start with an identifier.
Daniel Dunbar419aded2009-07-28 16:38:40 +0000355 AsmToken ID = Lexer.getTok();
356 SMLoc IDLoc = ID.getLoc();
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +0000357 StringRef IDVal;
358 if (ParseIdentifier(IDVal))
359 return TokError("unexpected token at start of statement");
360
361 // FIXME: Recurse on local labels?
362
363 // See what kind of statement we have.
364 switch (Lexer.getKind()) {
Daniel Dunbar3f872332009-07-28 16:08:33 +0000365 case AsmToken::Colon: {
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000366 // identifier ':' -> Label.
367 Lexer.Lex();
Daniel Dunbardce0f3c2009-06-29 23:43:14 +0000368
369 // Diagnose attempt to use a variable as a label.
370 //
371 // FIXME: Diagnostics. Note the location of the definition as a label.
372 // FIXME: This doesn't diagnose assignment to a symbol which has been
373 // implicitly marked as external.
374 MCSymbol *Sym = Ctx.GetOrCreateSymbol(IDVal);
Daniel Dunbar8906ff12009-08-22 07:22:36 +0000375 if (!Sym->isUndefined())
Daniel Dunbardce0f3c2009-06-29 23:43:14 +0000376 return Error(IDLoc, "invalid symbol redefinition");
Chris Lattnerc69485e2009-06-24 04:31:49 +0000377
378 // Since we saw a label, create a symbol and emit it.
379 // FIXME: If the label starts with L it is an assembler temporary label.
380 // Why does the client of this api need to know this?
Daniel Dunbardce0f3c2009-06-29 23:43:14 +0000381 Out.EmitLabel(Sym);
382
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000383 return ParseStatement();
Daniel Dunbardce0f3c2009-06-29 23:43:14 +0000384 }
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000385
Daniel Dunbar3f872332009-07-28 16:08:33 +0000386 case AsmToken::Equal:
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000387 // identifier '=' ... -> assignment statement
388 Lexer.Lex();
389
390 return ParseAssignment(IDVal, false);
391
392 default: // Normal instruction or directive.
393 break;
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000394 }
395
396 // Otherwise, we have a normal instruction or directive.
Chris Lattner2cf5f142009-06-22 01:29:09 +0000397 if (IDVal[0] == '.') {
Chris Lattner529fb542009-06-24 05:13:15 +0000398 // FIXME: This should be driven based on a hash lookup and callback.
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000399 if (IDVal == ".section")
Chris Lattner529fb542009-06-24 05:13:15 +0000400 return ParseDirectiveDarwinSection();
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000401 if (IDVal == ".text")
Chris Lattner529fb542009-06-24 05:13:15 +0000402 // FIXME: This changes behavior based on the -static flag to the
403 // assembler.
Chris Lattnerff4bc462009-08-10 01:39:42 +0000404 return ParseDirectiveSectionSwitch("__TEXT", "__text",
405 MCSectionMachO::S_ATTR_PURE_INSTRUCTIONS);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000406 if (IDVal == ".const")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000407 return ParseDirectiveSectionSwitch("__TEXT", "__const");
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000408 if (IDVal == ".static_const")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000409 return ParseDirectiveSectionSwitch("__TEXT", "__static_const");
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000410 if (IDVal == ".cstring")
Chris Lattnerff4bc462009-08-10 01:39:42 +0000411 return ParseDirectiveSectionSwitch("__TEXT","__cstring",
412 MCSectionMachO::S_CSTRING_LITERALS);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000413 if (IDVal == ".literal4")
Chris Lattnerff4bc462009-08-10 01:39:42 +0000414 return ParseDirectiveSectionSwitch("__TEXT", "__literal4",
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000415 MCSectionMachO::S_4BYTE_LITERALS,
416 4);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000417 if (IDVal == ".literal8")
Chris Lattnerff4bc462009-08-10 01:39:42 +0000418 return ParseDirectiveSectionSwitch("__TEXT", "__literal8",
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000419 MCSectionMachO::S_8BYTE_LITERALS,
420 8);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000421 if (IDVal == ".literal16")
Chris Lattnerff4bc462009-08-10 01:39:42 +0000422 return ParseDirectiveSectionSwitch("__TEXT","__literal16",
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000423 MCSectionMachO::S_16BYTE_LITERALS,
424 16);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000425 if (IDVal == ".constructor")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000426 return ParseDirectiveSectionSwitch("__TEXT","__constructor");
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000427 if (IDVal == ".destructor")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000428 return ParseDirectiveSectionSwitch("__TEXT","__destructor");
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000429 if (IDVal == ".fvmlib_init0")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000430 return ParseDirectiveSectionSwitch("__TEXT","__fvmlib_init0");
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000431 if (IDVal == ".fvmlib_init1")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000432 return ParseDirectiveSectionSwitch("__TEXT","__fvmlib_init1");
433
434 // FIXME: The assembler manual claims that this has the self modify code
435 // flag, at least on x86-32, but that does not appear to be correct.
Chris Lattnerff4bc462009-08-10 01:39:42 +0000436 if (IDVal == ".symbol_stub")
437 return ParseDirectiveSectionSwitch("__TEXT","__symbol_stub",
438 MCSectionMachO::S_SYMBOL_STUBS |
Chris Lattnerff4bc462009-08-10 01:39:42 +0000439 MCSectionMachO::S_ATTR_PURE_INSTRUCTIONS,
440 // FIXME: Different on PPC and ARM.
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000441 0, 16);
442 // FIXME: PowerPC only?
443 if (IDVal == ".picsymbol_stub")
444 return ParseDirectiveSectionSwitch("__TEXT","__picsymbol_stub",
445 MCSectionMachO::S_SYMBOL_STUBS |
446 MCSectionMachO::S_ATTR_PURE_INSTRUCTIONS,
447 0, 26);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000448 if (IDVal == ".data")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000449 return ParseDirectiveSectionSwitch("__DATA", "__data");
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000450 if (IDVal == ".static_data")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000451 return ParseDirectiveSectionSwitch("__DATA", "__static_data");
452
453 // FIXME: The section names of these two are misspelled in the assembler
454 // manual.
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000455 if (IDVal == ".non_lazy_symbol_pointer")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000456 return ParseDirectiveSectionSwitch("__DATA", "__nl_symbol_ptr",
457 MCSectionMachO::S_NON_LAZY_SYMBOL_POINTERS,
458 4);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000459 if (IDVal == ".lazy_symbol_pointer")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000460 return ParseDirectiveSectionSwitch("__DATA", "__la_symbol_ptr",
461 MCSectionMachO::S_LAZY_SYMBOL_POINTERS,
462 4);
463
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000464 if (IDVal == ".dyld")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000465 return ParseDirectiveSectionSwitch("__DATA", "__dyld");
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000466 if (IDVal == ".mod_init_func")
Chris Lattnerff4bc462009-08-10 01:39:42 +0000467 return ParseDirectiveSectionSwitch("__DATA", "__mod_init_func",
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000468 MCSectionMachO::S_MOD_INIT_FUNC_POINTERS,
469 4);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000470 if (IDVal == ".mod_term_func")
Chris Lattnerff4bc462009-08-10 01:39:42 +0000471 return ParseDirectiveSectionSwitch("__DATA", "__mod_term_func",
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000472 MCSectionMachO::S_MOD_TERM_FUNC_POINTERS,
473 4);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000474 if (IDVal == ".const_data")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000475 return ParseDirectiveSectionSwitch("__DATA", "__const");
Chris Lattner529fb542009-06-24 05:13:15 +0000476
477
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000478 if (IDVal == ".objc_class")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000479 return ParseDirectiveSectionSwitch("__OBJC", "__class",
480 MCSectionMachO::S_ATTR_NO_DEAD_STRIP);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000481 if (IDVal == ".objc_meta_class")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000482 return ParseDirectiveSectionSwitch("__OBJC", "__meta_class",
483 MCSectionMachO::S_ATTR_NO_DEAD_STRIP);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000484 if (IDVal == ".objc_cat_cls_meth")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000485 return ParseDirectiveSectionSwitch("__OBJC", "__cat_cls_meth",
486 MCSectionMachO::S_ATTR_NO_DEAD_STRIP);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000487 if (IDVal == ".objc_cat_inst_meth")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000488 return ParseDirectiveSectionSwitch("__OBJC", "__cat_inst_meth",
489 MCSectionMachO::S_ATTR_NO_DEAD_STRIP);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000490 if (IDVal == ".objc_protocol")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000491 return ParseDirectiveSectionSwitch("__OBJC", "__protocol",
492 MCSectionMachO::S_ATTR_NO_DEAD_STRIP);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000493 if (IDVal == ".objc_string_object")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000494 return ParseDirectiveSectionSwitch("__OBJC", "__string_object",
495 MCSectionMachO::S_ATTR_NO_DEAD_STRIP);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000496 if (IDVal == ".objc_cls_meth")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000497 return ParseDirectiveSectionSwitch("__OBJC", "__cls_meth",
498 MCSectionMachO::S_ATTR_NO_DEAD_STRIP);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000499 if (IDVal == ".objc_inst_meth")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000500 return ParseDirectiveSectionSwitch("__OBJC", "__inst_meth",
501 MCSectionMachO::S_ATTR_NO_DEAD_STRIP);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000502 if (IDVal == ".objc_cls_refs")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000503 return ParseDirectiveSectionSwitch("__OBJC", "__cls_refs",
504 MCSectionMachO::S_ATTR_NO_DEAD_STRIP |
505 MCSectionMachO::S_LITERAL_POINTERS,
506 4);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000507 if (IDVal == ".objc_message_refs")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000508 return ParseDirectiveSectionSwitch("__OBJC", "__message_refs",
509 MCSectionMachO::S_ATTR_NO_DEAD_STRIP |
510 MCSectionMachO::S_LITERAL_POINTERS,
511 4);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000512 if (IDVal == ".objc_symbols")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000513 return ParseDirectiveSectionSwitch("__OBJC", "__symbols",
514 MCSectionMachO::S_ATTR_NO_DEAD_STRIP);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000515 if (IDVal == ".objc_category")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000516 return ParseDirectiveSectionSwitch("__OBJC", "__category",
517 MCSectionMachO::S_ATTR_NO_DEAD_STRIP);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000518 if (IDVal == ".objc_class_vars")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000519 return ParseDirectiveSectionSwitch("__OBJC", "__class_vars",
520 MCSectionMachO::S_ATTR_NO_DEAD_STRIP);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000521 if (IDVal == ".objc_instance_vars")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000522 return ParseDirectiveSectionSwitch("__OBJC", "__instance_vars",
523 MCSectionMachO::S_ATTR_NO_DEAD_STRIP);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000524 if (IDVal == ".objc_module_info")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000525 return ParseDirectiveSectionSwitch("__OBJC", "__module_info",
526 MCSectionMachO::S_ATTR_NO_DEAD_STRIP);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000527 if (IDVal == ".objc_class_names")
Chris Lattnerff4bc462009-08-10 01:39:42 +0000528 return ParseDirectiveSectionSwitch("__TEXT", "__cstring",
529 MCSectionMachO::S_CSTRING_LITERALS);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000530 if (IDVal == ".objc_meth_var_types")
Chris Lattnerff4bc462009-08-10 01:39:42 +0000531 return ParseDirectiveSectionSwitch("__TEXT", "__cstring",
532 MCSectionMachO::S_CSTRING_LITERALS);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000533 if (IDVal == ".objc_meth_var_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_selector_strs")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000537 return ParseDirectiveSectionSwitch("__OBJC", "__selector_strs",
538 MCSectionMachO::S_CSTRING_LITERALS);
Chris Lattner9a023f72009-06-24 04:43:34 +0000539
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000540 // Assembler features
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000541 if (IDVal == ".set")
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000542 return ParseDirectiveSet();
543
Daniel Dunbara0d14262009-06-24 23:30:00 +0000544 // Data directives
545
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000546 if (IDVal == ".ascii")
Daniel Dunbara0d14262009-06-24 23:30:00 +0000547 return ParseDirectiveAscii(false);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000548 if (IDVal == ".asciz")
Daniel Dunbara0d14262009-06-24 23:30:00 +0000549 return ParseDirectiveAscii(true);
550
551 // FIXME: Target hooks for size? Also for "word", "hword".
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000552 if (IDVal == ".byte")
Daniel Dunbara0d14262009-06-24 23:30:00 +0000553 return ParseDirectiveValue(1);
Daniel Dunbar1e840b22009-08-11 04:44:00 +0000554 if (IDVal == ".short" || IDVal == ".word")
Daniel Dunbara0d14262009-06-24 23:30:00 +0000555 return ParseDirectiveValue(2);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000556 if (IDVal == ".long")
Daniel Dunbara0d14262009-06-24 23:30:00 +0000557 return ParseDirectiveValue(4);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000558 if (IDVal == ".quad")
Daniel Dunbara0d14262009-06-24 23:30:00 +0000559 return ParseDirectiveValue(8);
Daniel Dunbarc29dfa72009-06-29 23:46:59 +0000560
561 // FIXME: Target hooks for IsPow2.
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000562 if (IDVal == ".align")
Daniel Dunbarc29dfa72009-06-29 23:46:59 +0000563 return ParseDirectiveAlign(/*IsPow2=*/true, /*ExprSize=*/1);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000564 if (IDVal == ".align32")
Daniel Dunbarc29dfa72009-06-29 23:46:59 +0000565 return ParseDirectiveAlign(/*IsPow2=*/true, /*ExprSize=*/4);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000566 if (IDVal == ".balign")
Daniel Dunbarc29dfa72009-06-29 23:46:59 +0000567 return ParseDirectiveAlign(/*IsPow2=*/false, /*ExprSize=*/1);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000568 if (IDVal == ".balignw")
Daniel Dunbarc29dfa72009-06-29 23:46:59 +0000569 return ParseDirectiveAlign(/*IsPow2=*/false, /*ExprSize=*/2);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000570 if (IDVal == ".balignl")
Daniel Dunbarc29dfa72009-06-29 23:46:59 +0000571 return ParseDirectiveAlign(/*IsPow2=*/false, /*ExprSize=*/4);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000572 if (IDVal == ".p2align")
Daniel Dunbarc29dfa72009-06-29 23:46:59 +0000573 return ParseDirectiveAlign(/*IsPow2=*/true, /*ExprSize=*/1);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000574 if (IDVal == ".p2alignw")
Daniel Dunbarc29dfa72009-06-29 23:46:59 +0000575 return ParseDirectiveAlign(/*IsPow2=*/true, /*ExprSize=*/2);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000576 if (IDVal == ".p2alignl")
Daniel Dunbarc29dfa72009-06-29 23:46:59 +0000577 return ParseDirectiveAlign(/*IsPow2=*/true, /*ExprSize=*/4);
578
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000579 if (IDVal == ".org")
Daniel Dunbarc238b582009-06-25 22:44:51 +0000580 return ParseDirectiveOrg();
Daniel Dunbarc29dfa72009-06-29 23:46:59 +0000581
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000582 if (IDVal == ".fill")
Daniel Dunbarc29dfa72009-06-29 23:46:59 +0000583 return ParseDirectiveFill();
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000584 if (IDVal == ".space")
Daniel Dunbara0d14262009-06-24 23:30:00 +0000585 return ParseDirectiveSpace();
586
Daniel Dunbard7b267b2009-06-30 00:33:19 +0000587 // Symbol attribute directives
Daniel Dunbard0c14d62009-08-11 04:24:50 +0000588
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000589 if (IDVal == ".globl" || IDVal == ".global")
Daniel Dunbard7b267b2009-06-30 00:33:19 +0000590 return ParseDirectiveSymbolAttribute(MCStreamer::Global);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000591 if (IDVal == ".hidden")
Daniel Dunbard7b267b2009-06-30 00:33:19 +0000592 return ParseDirectiveSymbolAttribute(MCStreamer::Hidden);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000593 if (IDVal == ".indirect_symbol")
Daniel Dunbard7b267b2009-06-30 00:33:19 +0000594 return ParseDirectiveSymbolAttribute(MCStreamer::IndirectSymbol);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000595 if (IDVal == ".internal")
Daniel Dunbard7b267b2009-06-30 00:33:19 +0000596 return ParseDirectiveSymbolAttribute(MCStreamer::Internal);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000597 if (IDVal == ".lazy_reference")
Daniel Dunbard7b267b2009-06-30 00:33:19 +0000598 return ParseDirectiveSymbolAttribute(MCStreamer::LazyReference);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000599 if (IDVal == ".no_dead_strip")
Daniel Dunbard7b267b2009-06-30 00:33:19 +0000600 return ParseDirectiveSymbolAttribute(MCStreamer::NoDeadStrip);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000601 if (IDVal == ".private_extern")
Daniel Dunbard7b267b2009-06-30 00:33:19 +0000602 return ParseDirectiveSymbolAttribute(MCStreamer::PrivateExtern);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000603 if (IDVal == ".protected")
Daniel Dunbard7b267b2009-06-30 00:33:19 +0000604 return ParseDirectiveSymbolAttribute(MCStreamer::Protected);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000605 if (IDVal == ".reference")
Daniel Dunbard7b267b2009-06-30 00:33:19 +0000606 return ParseDirectiveSymbolAttribute(MCStreamer::Reference);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000607 if (IDVal == ".weak")
Daniel Dunbard7b267b2009-06-30 00:33:19 +0000608 return ParseDirectiveSymbolAttribute(MCStreamer::Weak);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000609 if (IDVal == ".weak_definition")
Daniel Dunbard7b267b2009-06-30 00:33:19 +0000610 return ParseDirectiveSymbolAttribute(MCStreamer::WeakDefinition);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000611 if (IDVal == ".weak_reference")
Daniel Dunbard7b267b2009-06-30 00:33:19 +0000612 return ParseDirectiveSymbolAttribute(MCStreamer::WeakReference);
613
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000614 if (IDVal == ".comm")
Chris Lattner1fc3d752009-07-09 17:25:12 +0000615 return ParseDirectiveComm(/*IsLocal=*/false);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000616 if (IDVal == ".lcomm")
Chris Lattner1fc3d752009-07-09 17:25:12 +0000617 return ParseDirectiveComm(/*IsLocal=*/true);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000618 if (IDVal == ".zerofill")
Chris Lattner9be3fee2009-07-10 22:20:30 +0000619 return ParseDirectiveDarwinZerofill();
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000620 if (IDVal == ".desc")
Kevin Enderby95cf30c2009-07-14 18:17:10 +0000621 return ParseDirectiveDarwinSymbolDesc();
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000622 if (IDVal == ".lsym")
Kevin Enderby71148242009-07-14 21:35:03 +0000623 return ParseDirectiveDarwinLsym();
Chris Lattner4e4db7a2009-07-07 20:30:46 +0000624
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000625 if (IDVal == ".subsections_via_symbols")
Kevin Enderbya5c78322009-07-13 21:03:15 +0000626 return ParseDirectiveDarwinSubsectionsViaSymbols();
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000627 if (IDVal == ".abort")
Kevin Enderby5f1f0b82009-07-13 23:15:14 +0000628 return ParseDirectiveAbort();
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000629 if (IDVal == ".include")
Kevin Enderby1f049b22009-07-14 23:21:55 +0000630 return ParseDirectiveInclude();
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000631 if (IDVal == ".dump")
Kevin Enderby5026ae42009-07-20 20:25:37 +0000632 return ParseDirectiveDarwinDumpOrLoad(IDLoc, /*IsDump=*/true);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000633 if (IDVal == ".load")
Kevin Enderby5026ae42009-07-20 20:25:37 +0000634 return ParseDirectiveDarwinDumpOrLoad(IDLoc, /*IsLoad=*/false);
Kevin Enderbya5c78322009-07-13 21:03:15 +0000635
Daniel Dunbard0c14d62009-08-11 04:24:50 +0000636 // Debugging directives
637
638 if (IDVal == ".file")
639 return ParseDirectiveFile(IDLoc);
640 if (IDVal == ".line")
641 return ParseDirectiveLine(IDLoc);
642 if (IDVal == ".loc")
643 return ParseDirectiveLoc(IDLoc);
644
Daniel Dunbar3fb76832009-06-30 00:49:23 +0000645 Warning(IDLoc, "ignoring directive for now");
Chris Lattner2cf5f142009-06-22 01:29:09 +0000646 EatToEndOfStatement();
647 return false;
648 }
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000649
Chris Lattner29dfe7c2009-06-23 18:41:30 +0000650 MCInst Inst;
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000651 if (getTargetParser().ParseInstruction(IDVal, Inst))
Chris Lattner29dfe7c2009-06-23 18:41:30 +0000652 return true;
Chris Lattner2cf5f142009-06-22 01:29:09 +0000653
Daniel Dunbar3f872332009-07-28 16:08:33 +0000654 if (Lexer.isNot(AsmToken::EndOfStatement))
Chris Lattner9a023f72009-06-24 04:43:34 +0000655 return TokError("unexpected token in argument list");
Chris Lattner2cf5f142009-06-22 01:29:09 +0000656
657 // Eat the end of statement marker.
658 Lexer.Lex();
659
660 // Instruction is good, process it.
Daniel Dunbar0eebb052009-07-01 06:35:48 +0000661 Out.EmitInstruction(Inst);
Chris Lattner2cf5f142009-06-22 01:29:09 +0000662
663 // Skip to end of line for now.
Chris Lattner27aa7d22009-06-21 20:16:42 +0000664 return false;
665}
Chris Lattner9a023f72009-06-24 04:43:34 +0000666
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000667bool AsmParser::ParseAssignment(const StringRef &Name, bool IsDotSet) {
Daniel Dunbardce0f3c2009-06-29 23:43:14 +0000668 // FIXME: Use better location, we should use proper tokens.
669 SMLoc EqualLoc = Lexer.getLoc();
670
Daniel Dunbarf4b830f2009-06-30 02:10:03 +0000671 MCValue Value;
672 if (ParseRelocatableExpression(Value))
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000673 return true;
674
Daniel Dunbar3f872332009-07-28 16:08:33 +0000675 if (Lexer.isNot(AsmToken::EndOfStatement))
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000676 return TokError("unexpected token in assignment");
677
678 // Eat the end of statement marker.
679 Lexer.Lex();
680
Daniel Dunbardce0f3c2009-06-29 23:43:14 +0000681 // Diagnose assignment to a label.
682 //
683 // FIXME: Diagnostics. Note the location of the definition as a label.
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000684 // FIXME: Handle '.'.
Daniel Dunbardce0f3c2009-06-29 23:43:14 +0000685 // FIXME: Diagnose assignment to protected identifier (e.g., register name).
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000686 MCSymbol *Sym = Ctx.GetOrCreateSymbol(Name);
Daniel Dunbar8906ff12009-08-22 07:22:36 +0000687 if (!Sym->isUndefined() && !Sym->isAbsolute())
688 return Error(EqualLoc, "symbol has already been defined");
Daniel Dunbardce0f3c2009-06-29 23:43:14 +0000689
690 // Do the assignment.
Daniel Dunbarf4b830f2009-06-30 02:10:03 +0000691 Out.EmitAssignment(Sym, Value, IsDotSet);
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000692
693 return false;
694}
695
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +0000696/// ParseIdentifier:
697/// ::= identifier
698/// ::= string
699bool AsmParser::ParseIdentifier(StringRef &Res) {
700 if (Lexer.isNot(AsmToken::Identifier) &&
701 Lexer.isNot(AsmToken::String))
702 return true;
703
704 Res = Lexer.getTok().getIdentifier();
705
706 Lexer.Lex(); // Consume the identifier token.
707
708 return false;
709}
710
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000711/// ParseDirectiveSet:
712/// ::= .set identifier ',' expression
713bool AsmParser::ParseDirectiveSet() {
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +0000714 StringRef Name;
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000715
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +0000716 if (ParseIdentifier(Name))
717 return TokError("expected identifier after '.set' directive");
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000718
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +0000719 if (Lexer.isNot(AsmToken::Comma))
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000720 return TokError("unexpected token in '.set'");
721 Lexer.Lex();
722
723 return ParseAssignment(Name, true);
724}
725
Chris Lattner9a023f72009-06-24 04:43:34 +0000726/// ParseDirectiveSection:
Chris Lattner529fb542009-06-24 05:13:15 +0000727/// ::= .section identifier (',' identifier)*
728/// FIXME: This should actually parse out the segment, section, attributes and
729/// sizeof_stub fields.
730bool AsmParser::ParseDirectiveDarwinSection() {
Daniel Dunbarace63122009-08-11 03:42:33 +0000731 SMLoc Loc = Lexer.getLoc();
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +0000732
Daniel Dunbarace63122009-08-11 03:42:33 +0000733 StringRef SectionName;
734 if (ParseIdentifier(SectionName))
735 return Error(Loc, "expected identifier after '.section' directive");
736
737 // Verify there is a following comma.
738 if (!Lexer.is(AsmToken::Comma))
739 return TokError("unexpected token in '.section' directive");
740
Chris Lattnerff4bc462009-08-10 01:39:42 +0000741 std::string SectionSpec = SectionName;
Daniel Dunbarace63122009-08-11 03:42:33 +0000742 SectionSpec += ",";
743
744 // Add all the tokens until the end of the line, ParseSectionSpecifier will
745 // handle this.
Chris Lattnerff4bc462009-08-10 01:39:42 +0000746 StringRef EOL = Lexer.LexUntilEndOfStatement();
747 SectionSpec.append(EOL.begin(), EOL.end());
Daniel Dunbarace63122009-08-11 03:42:33 +0000748
Chris Lattnerff4bc462009-08-10 01:39:42 +0000749 Lexer.Lex();
Daniel Dunbar3f872332009-07-28 16:08:33 +0000750 if (Lexer.isNot(AsmToken::EndOfStatement))
Chris Lattner9a023f72009-06-24 04:43:34 +0000751 return TokError("unexpected token in '.section' directive");
752 Lexer.Lex();
753
Chris Lattnerff4bc462009-08-10 01:39:42 +0000754
755 StringRef Segment, Section;
756 unsigned TAA, StubSize;
757 std::string ErrorStr =
758 MCSectionMachO::ParseSectionSpecifier(SectionSpec, Segment, Section,
759 TAA, StubSize);
760
761 if (!ErrorStr.empty())
Daniel Dunbarace63122009-08-11 03:42:33 +0000762 return Error(Loc, ErrorStr.c_str());
Chris Lattnerff4bc462009-08-10 01:39:42 +0000763
764 // FIXME: CACHE THESE.
765
Chris Lattner56594f92009-07-31 17:47:16 +0000766 // FIXME: Arch specific.
Chris Lattnerff4bc462009-08-10 01:39:42 +0000767 MCSection *S = 0; //Ctx.GetSection(Section);
Chris Lattner56594f92009-07-31 17:47:16 +0000768 if (S == 0)
Chris Lattnerff4bc462009-08-10 01:39:42 +0000769 S = MCSectionMachO::Create(Segment, Section, TAA, StubSize,
770 SectionKind(), Ctx);
Chris Lattner56594f92009-07-31 17:47:16 +0000771
772 Out.SwitchSection(S);
Chris Lattner9a023f72009-06-24 04:43:34 +0000773 return false;
774}
775
Chris Lattnere15c2d72009-08-10 18:05:55 +0000776/// ParseDirectiveSectionSwitch -
Chris Lattnerff4bc462009-08-10 01:39:42 +0000777bool AsmParser::ParseDirectiveSectionSwitch(const char *Segment,
778 const char *Section,
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000779 unsigned TAA, unsigned Align,
780 unsigned StubSize) {
Daniel Dunbar3f872332009-07-28 16:08:33 +0000781 if (Lexer.isNot(AsmToken::EndOfStatement))
Chris Lattner529fb542009-06-24 05:13:15 +0000782 return TokError("unexpected token in section switching directive");
783 Lexer.Lex();
784
Chris Lattner56594f92009-07-31 17:47:16 +0000785 // FIXME: Arch specific.
Chris Lattnerff4bc462009-08-10 01:39:42 +0000786 // FIXME: Cache this!
787 MCSection *S = 0; // Ctx.GetSection(Section);
Chris Lattner56594f92009-07-31 17:47:16 +0000788 if (S == 0)
Chris Lattnerff4bc462009-08-10 01:39:42 +0000789 S = MCSectionMachO::Create(Segment, Section, TAA, StubSize,
790 SectionKind(), Ctx);
Chris Lattner56594f92009-07-31 17:47:16 +0000791
792 Out.SwitchSection(S);
Daniel Dunbar2330df62009-08-21 23:30:15 +0000793
794 // Set the implicit alignment, if any.
795 //
796 // FIXME: This isn't really what 'as' does; I think it just uses the implicit
797 // alignment on the section (e.g., if one manually inserts bytes into the
798 // section, then just issueing the section switch directive will not realign
799 // the section. However, this is arguably more reasonable behavior, and there
800 // is no good reason for someone to intentionally emit incorrectly sized
801 // values into the implicitly aligned sections.
802 if (Align)
803 Out.EmitValueToAlignment(Align, 0, 1, 0);
804
Chris Lattner529fb542009-06-24 05:13:15 +0000805 return false;
806}
Daniel Dunbara0d14262009-06-24 23:30:00 +0000807
Daniel Dunbar1ab75942009-08-14 18:19:52 +0000808bool AsmParser::ParseEscapedString(std::string &Data) {
809 assert(Lexer.is(AsmToken::String) && "Unexpected current token!");
810
811 Data = "";
812 StringRef Str = Lexer.getTok().getStringContents();
813 for (unsigned i = 0, e = Str.size(); i != e; ++i) {
814 if (Str[i] != '\\') {
815 Data += Str[i];
816 continue;
817 }
818
819 // Recognize escaped characters. Note that this escape semantics currently
820 // loosely follows Darwin 'as'. Notably, it doesn't support hex escapes.
821 ++i;
822 if (i == e)
823 return TokError("unexpected backslash at end of string");
824
825 // Recognize octal sequences.
826 if ((unsigned) (Str[i] - '0') <= 7) {
827 // Consume up to three octal characters.
828 unsigned Value = Str[i] - '0';
829
830 if (i + 1 != e && ((unsigned) (Str[i + 1] - '0')) <= 7) {
831 ++i;
832 Value = Value * 8 + (Str[i] - '0');
833
834 if (i + 1 != e && ((unsigned) (Str[i + 1] - '0')) <= 7) {
835 ++i;
836 Value = Value * 8 + (Str[i] - '0');
837 }
838 }
839
840 if (Value > 255)
841 return TokError("invalid octal escape sequence (out of range)");
842
843 Data += (unsigned char) Value;
844 continue;
845 }
846
847 // Otherwise recognize individual escapes.
848 switch (Str[i]) {
849 default:
850 // Just reject invalid escape sequences for now.
851 return TokError("invalid escape sequence (unrecognized character)");
852
853 case 'b': Data += '\b'; break;
854 case 'f': Data += '\f'; break;
855 case 'n': Data += '\n'; break;
856 case 'r': Data += '\r'; break;
857 case 't': Data += '\t'; break;
858 case '"': Data += '"'; break;
859 case '\\': Data += '\\'; break;
860 }
861 }
862
863 return false;
864}
865
Daniel Dunbara0d14262009-06-24 23:30:00 +0000866/// ParseDirectiveAscii:
Daniel Dunbar475839e2009-06-29 20:37:27 +0000867/// ::= ( .ascii | .asciz ) [ "string" ( , "string" )* ]
Daniel Dunbara0d14262009-06-24 23:30:00 +0000868bool AsmParser::ParseDirectiveAscii(bool ZeroTerminated) {
Daniel Dunbar3f872332009-07-28 16:08:33 +0000869 if (Lexer.isNot(AsmToken::EndOfStatement)) {
Daniel Dunbara0d14262009-06-24 23:30:00 +0000870 for (;;) {
Daniel Dunbar3f872332009-07-28 16:08:33 +0000871 if (Lexer.isNot(AsmToken::String))
Daniel Dunbara0d14262009-06-24 23:30:00 +0000872 return TokError("expected string in '.ascii' or '.asciz' directive");
873
Daniel Dunbar1ab75942009-08-14 18:19:52 +0000874 std::string Data;
875 if (ParseEscapedString(Data))
876 return true;
877
878 Out.EmitBytes(Data);
Daniel Dunbara0d14262009-06-24 23:30:00 +0000879 if (ZeroTerminated)
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000880 Out.EmitBytes(StringRef("\0", 1));
Daniel Dunbara0d14262009-06-24 23:30:00 +0000881
882 Lexer.Lex();
883
Daniel Dunbar3f872332009-07-28 16:08:33 +0000884 if (Lexer.is(AsmToken::EndOfStatement))
Daniel Dunbara0d14262009-06-24 23:30:00 +0000885 break;
886
Daniel Dunbar3f872332009-07-28 16:08:33 +0000887 if (Lexer.isNot(AsmToken::Comma))
Daniel Dunbara0d14262009-06-24 23:30:00 +0000888 return TokError("unexpected token in '.ascii' or '.asciz' directive");
889 Lexer.Lex();
890 }
891 }
892
893 Lexer.Lex();
894 return false;
895}
896
897/// ParseDirectiveValue
898/// ::= (.byte | .short | ... ) [ expression (, expression)* ]
899bool AsmParser::ParseDirectiveValue(unsigned Size) {
Daniel Dunbar3f872332009-07-28 16:08:33 +0000900 if (Lexer.isNot(AsmToken::EndOfStatement)) {
Daniel Dunbara0d14262009-06-24 23:30:00 +0000901 for (;;) {
Daniel Dunbarf4b830f2009-06-30 02:10:03 +0000902 MCValue Expr;
903 if (ParseRelocatableExpression(Expr))
Daniel Dunbara0d14262009-06-24 23:30:00 +0000904 return true;
905
Daniel Dunbarf4b830f2009-06-30 02:10:03 +0000906 Out.EmitValue(Expr, Size);
Daniel Dunbara0d14262009-06-24 23:30:00 +0000907
Daniel Dunbar3f872332009-07-28 16:08:33 +0000908 if (Lexer.is(AsmToken::EndOfStatement))
Daniel Dunbara0d14262009-06-24 23:30:00 +0000909 break;
910
911 // FIXME: Improve diagnostic.
Daniel Dunbar3f872332009-07-28 16:08:33 +0000912 if (Lexer.isNot(AsmToken::Comma))
Daniel Dunbara0d14262009-06-24 23:30:00 +0000913 return TokError("unexpected token in directive");
914 Lexer.Lex();
915 }
916 }
917
918 Lexer.Lex();
919 return false;
920}
921
922/// ParseDirectiveSpace
923/// ::= .space expression [ , expression ]
924bool AsmParser::ParseDirectiveSpace() {
925 int64_t NumBytes;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000926 if (ParseAbsoluteExpression(NumBytes))
Daniel Dunbara0d14262009-06-24 23:30:00 +0000927 return true;
928
929 int64_t FillExpr = 0;
930 bool HasFillExpr = false;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000931 if (Lexer.isNot(AsmToken::EndOfStatement)) {
932 if (Lexer.isNot(AsmToken::Comma))
Daniel Dunbara0d14262009-06-24 23:30:00 +0000933 return TokError("unexpected token in '.space' directive");
934 Lexer.Lex();
935
Daniel Dunbar475839e2009-06-29 20:37:27 +0000936 if (ParseAbsoluteExpression(FillExpr))
Daniel Dunbara0d14262009-06-24 23:30:00 +0000937 return true;
938
939 HasFillExpr = true;
940
Daniel Dunbar3f872332009-07-28 16:08:33 +0000941 if (Lexer.isNot(AsmToken::EndOfStatement))
Daniel Dunbara0d14262009-06-24 23:30:00 +0000942 return TokError("unexpected token in '.space' directive");
943 }
944
945 Lexer.Lex();
946
947 if (NumBytes <= 0)
948 return TokError("invalid number of bytes in '.space' directive");
949
950 // FIXME: Sometimes the fill expr is 'nop' if it isn't supplied, instead of 0.
951 for (uint64_t i = 0, e = NumBytes; i != e; ++i)
952 Out.EmitValue(MCValue::get(FillExpr), 1);
953
954 return false;
955}
956
957/// ParseDirectiveFill
958/// ::= .fill expression , expression , expression
959bool AsmParser::ParseDirectiveFill() {
960 int64_t NumValues;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000961 if (ParseAbsoluteExpression(NumValues))
Daniel Dunbara0d14262009-06-24 23:30:00 +0000962 return true;
963
Daniel Dunbar3f872332009-07-28 16:08:33 +0000964 if (Lexer.isNot(AsmToken::Comma))
Daniel Dunbara0d14262009-06-24 23:30:00 +0000965 return TokError("unexpected token in '.fill' directive");
966 Lexer.Lex();
967
968 int64_t FillSize;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000969 if (ParseAbsoluteExpression(FillSize))
Daniel Dunbara0d14262009-06-24 23:30:00 +0000970 return true;
971
Daniel Dunbar3f872332009-07-28 16:08:33 +0000972 if (Lexer.isNot(AsmToken::Comma))
Daniel Dunbara0d14262009-06-24 23:30:00 +0000973 return TokError("unexpected token in '.fill' directive");
974 Lexer.Lex();
975
976 int64_t FillExpr;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000977 if (ParseAbsoluteExpression(FillExpr))
Daniel Dunbara0d14262009-06-24 23:30:00 +0000978 return true;
979
Daniel Dunbar3f872332009-07-28 16:08:33 +0000980 if (Lexer.isNot(AsmToken::EndOfStatement))
Daniel Dunbara0d14262009-06-24 23:30:00 +0000981 return TokError("unexpected token in '.fill' directive");
982
983 Lexer.Lex();
984
Daniel Dunbarbc38ca72009-08-21 15:43:35 +0000985 if (FillSize != 1 && FillSize != 2 && FillSize != 4 && FillSize != 8)
986 return TokError("invalid '.fill' size, expected 1, 2, 4, or 8");
Daniel Dunbara0d14262009-06-24 23:30:00 +0000987
988 for (uint64_t i = 0, e = NumValues; i != e; ++i)
989 Out.EmitValue(MCValue::get(FillExpr), FillSize);
990
991 return false;
992}
Daniel Dunbarc238b582009-06-25 22:44:51 +0000993
994/// ParseDirectiveOrg
995/// ::= .org expression [ , expression ]
996bool AsmParser::ParseDirectiveOrg() {
Daniel Dunbarf4b830f2009-06-30 02:10:03 +0000997 MCValue Offset;
998 if (ParseRelocatableExpression(Offset))
Daniel Dunbarc238b582009-06-25 22:44:51 +0000999 return true;
1000
1001 // Parse optional fill expression.
1002 int64_t FillExpr = 0;
Daniel Dunbar3f872332009-07-28 16:08:33 +00001003 if (Lexer.isNot(AsmToken::EndOfStatement)) {
1004 if (Lexer.isNot(AsmToken::Comma))
Daniel Dunbarc238b582009-06-25 22:44:51 +00001005 return TokError("unexpected token in '.org' directive");
1006 Lexer.Lex();
1007
Daniel Dunbar475839e2009-06-29 20:37:27 +00001008 if (ParseAbsoluteExpression(FillExpr))
Daniel Dunbarc238b582009-06-25 22:44:51 +00001009 return true;
1010
Daniel Dunbar3f872332009-07-28 16:08:33 +00001011 if (Lexer.isNot(AsmToken::EndOfStatement))
Daniel Dunbarc238b582009-06-25 22:44:51 +00001012 return TokError("unexpected token in '.org' directive");
1013 }
1014
1015 Lexer.Lex();
Daniel Dunbarf4b830f2009-06-30 02:10:03 +00001016
1017 // FIXME: Only limited forms of relocatable expressions are accepted here, it
1018 // has to be relative to the current section.
1019 Out.EmitValueToOffset(Offset, FillExpr);
Daniel Dunbarc238b582009-06-25 22:44:51 +00001020
1021 return false;
1022}
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001023
1024/// ParseDirectiveAlign
1025/// ::= {.align, ...} expression [ , expression [ , expression ]]
1026bool AsmParser::ParseDirectiveAlign(bool IsPow2, unsigned ValueSize) {
Daniel Dunbarb58a8042009-08-26 09:16:34 +00001027 SMLoc AlignmentLoc = Lexer.getLoc();
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001028 int64_t Alignment;
1029 if (ParseAbsoluteExpression(Alignment))
1030 return true;
1031
1032 SMLoc MaxBytesLoc;
1033 bool HasFillExpr = false;
1034 int64_t FillExpr = 0;
1035 int64_t MaxBytesToFill = 0;
Daniel Dunbar3f872332009-07-28 16:08:33 +00001036 if (Lexer.isNot(AsmToken::EndOfStatement)) {
1037 if (Lexer.isNot(AsmToken::Comma))
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001038 return TokError("unexpected token in directive");
1039 Lexer.Lex();
1040
1041 // The fill expression can be omitted while specifying a maximum number of
1042 // alignment bytes, e.g:
1043 // .align 3,,4
Daniel Dunbar3f872332009-07-28 16:08:33 +00001044 if (Lexer.isNot(AsmToken::Comma)) {
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001045 HasFillExpr = true;
1046 if (ParseAbsoluteExpression(FillExpr))
1047 return true;
1048 }
1049
Daniel Dunbar3f872332009-07-28 16:08:33 +00001050 if (Lexer.isNot(AsmToken::EndOfStatement)) {
1051 if (Lexer.isNot(AsmToken::Comma))
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001052 return TokError("unexpected token in directive");
1053 Lexer.Lex();
1054
1055 MaxBytesLoc = Lexer.getLoc();
1056 if (ParseAbsoluteExpression(MaxBytesToFill))
1057 return true;
1058
Daniel Dunbar3f872332009-07-28 16:08:33 +00001059 if (Lexer.isNot(AsmToken::EndOfStatement))
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001060 return TokError("unexpected token in directive");
1061 }
1062 }
1063
1064 Lexer.Lex();
1065
1066 if (!HasFillExpr) {
1067 // FIXME: Sometimes fill with nop.
1068 FillExpr = 0;
1069 }
1070
1071 // Compute alignment in bytes.
1072 if (IsPow2) {
1073 // FIXME: Diagnose overflow.
Daniel Dunbarb58a8042009-08-26 09:16:34 +00001074 if (Alignment >= 32) {
1075 Error(AlignmentLoc, "invalid alignment value");
1076 Alignment = 31;
1077 }
1078
1079 Alignment = 1 << Alignment;
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001080 }
1081
Daniel Dunbarb58a8042009-08-26 09:16:34 +00001082 // Diagnose non-sensical max bytes to align.
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001083 if (MaxBytesLoc.isValid()) {
1084 if (MaxBytesToFill < 1) {
Daniel Dunbarb58a8042009-08-26 09:16:34 +00001085 Error(MaxBytesLoc, "alignment directive can never be satisfied in this "
1086 "many bytes, ignoring maximum bytes expression");
Daniel Dunbar0afb9f52009-08-21 23:01:53 +00001087 MaxBytesToFill = 0;
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001088 }
1089
1090 if (MaxBytesToFill >= Alignment) {
Daniel Dunbar3fb76832009-06-30 00:49:23 +00001091 Warning(MaxBytesLoc, "maximum bytes expression exceeds alignment and "
1092 "has no effect");
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001093 MaxBytesToFill = 0;
1094 }
1095 }
1096
1097 // FIXME: Target specific behavior about how the "extra" bytes are filled.
1098 Out.EmitValueToAlignment(Alignment, FillExpr, ValueSize, MaxBytesToFill);
1099
1100 return false;
1101}
1102
Daniel Dunbard7b267b2009-06-30 00:33:19 +00001103/// ParseDirectiveSymbolAttribute
1104/// ::= { ".globl", ".weak", ... } [ identifier ( , identifier )* ]
1105bool AsmParser::ParseDirectiveSymbolAttribute(MCStreamer::SymbolAttr Attr) {
Daniel Dunbar3f872332009-07-28 16:08:33 +00001106 if (Lexer.isNot(AsmToken::EndOfStatement)) {
Daniel Dunbard7b267b2009-06-30 00:33:19 +00001107 for (;;) {
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00001108 StringRef Name;
1109
1110 if (ParseIdentifier(Name))
Daniel Dunbard7b267b2009-06-30 00:33:19 +00001111 return TokError("expected identifier in directive");
1112
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00001113 MCSymbol *Sym = Ctx.GetOrCreateSymbol(Name);
Daniel Dunbard7b267b2009-06-30 00:33:19 +00001114
Daniel Dunbard7b267b2009-06-30 00:33:19 +00001115 Out.EmitSymbolAttribute(Sym, Attr);
1116
Daniel Dunbar3f872332009-07-28 16:08:33 +00001117 if (Lexer.is(AsmToken::EndOfStatement))
Daniel Dunbard7b267b2009-06-30 00:33:19 +00001118 break;
1119
Daniel Dunbar3f872332009-07-28 16:08:33 +00001120 if (Lexer.isNot(AsmToken::Comma))
Daniel Dunbard7b267b2009-06-30 00:33:19 +00001121 return TokError("unexpected token in directive");
1122 Lexer.Lex();
1123 }
1124 }
1125
1126 Lexer.Lex();
1127 return false;
1128}
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001129
Kevin Enderby95cf30c2009-07-14 18:17:10 +00001130/// ParseDirectiveDarwinSymbolDesc
1131/// ::= .desc identifier , expression
1132bool AsmParser::ParseDirectiveDarwinSymbolDesc() {
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00001133 StringRef Name;
1134 if (ParseIdentifier(Name))
Kevin Enderby95cf30c2009-07-14 18:17:10 +00001135 return TokError("expected identifier in directive");
1136
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00001137 // Handle the identifier as the key symbol.
1138 MCSymbol *Sym = Ctx.GetOrCreateSymbol(Name);
Kevin Enderby95cf30c2009-07-14 18:17:10 +00001139
Daniel Dunbar3f872332009-07-28 16:08:33 +00001140 if (Lexer.isNot(AsmToken::Comma))
Kevin Enderby95cf30c2009-07-14 18:17:10 +00001141 return TokError("unexpected token in '.desc' directive");
1142 Lexer.Lex();
1143
1144 SMLoc DescLoc = Lexer.getLoc();
1145 int64_t DescValue;
1146 if (ParseAbsoluteExpression(DescValue))
1147 return true;
1148
Daniel Dunbar3f872332009-07-28 16:08:33 +00001149 if (Lexer.isNot(AsmToken::EndOfStatement))
Kevin Enderby95cf30c2009-07-14 18:17:10 +00001150 return TokError("unexpected token in '.desc' directive");
1151
1152 Lexer.Lex();
1153
1154 // Set the n_desc field of this Symbol to this DescValue
1155 Out.EmitSymbolDesc(Sym, DescValue);
1156
1157 return false;
1158}
1159
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001160/// ParseDirectiveComm
Chris Lattner1fc3d752009-07-09 17:25:12 +00001161/// ::= ( .comm | .lcomm ) identifier , size_expression [ , align_expression ]
1162bool AsmParser::ParseDirectiveComm(bool IsLocal) {
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00001163 SMLoc IDLoc = Lexer.getLoc();
1164 StringRef Name;
1165 if (ParseIdentifier(Name))
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001166 return TokError("expected identifier in directive");
1167
Daniel Dunbar76c4d762009-07-31 21:55:09 +00001168 // Handle the identifier as the key symbol.
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00001169 MCSymbol *Sym = Ctx.GetOrCreateSymbol(Name);
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001170
Daniel Dunbar3f872332009-07-28 16:08:33 +00001171 if (Lexer.isNot(AsmToken::Comma))
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001172 return TokError("unexpected token in directive");
1173 Lexer.Lex();
1174
1175 int64_t Size;
1176 SMLoc SizeLoc = Lexer.getLoc();
1177 if (ParseAbsoluteExpression(Size))
1178 return true;
1179
1180 int64_t Pow2Alignment = 0;
1181 SMLoc Pow2AlignmentLoc;
Daniel Dunbar3f872332009-07-28 16:08:33 +00001182 if (Lexer.is(AsmToken::Comma)) {
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001183 Lexer.Lex();
1184 Pow2AlignmentLoc = Lexer.getLoc();
1185 if (ParseAbsoluteExpression(Pow2Alignment))
1186 return true;
1187 }
1188
Daniel Dunbar3f872332009-07-28 16:08:33 +00001189 if (Lexer.isNot(AsmToken::EndOfStatement))
Chris Lattner1fc3d752009-07-09 17:25:12 +00001190 return TokError("unexpected token in '.comm' or '.lcomm' directive");
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001191
1192 Lexer.Lex();
1193
Chris Lattner1fc3d752009-07-09 17:25:12 +00001194 // NOTE: a size of zero for a .comm should create a undefined symbol
1195 // but a size of .lcomm creates a bss symbol of size zero.
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001196 if (Size < 0)
Chris Lattner1fc3d752009-07-09 17:25:12 +00001197 return Error(SizeLoc, "invalid '.comm' or '.lcomm' directive size, can't "
1198 "be less than zero");
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001199
1200 // NOTE: The alignment in the directive is a power of 2 value, the assember
1201 // may internally end up wanting an alignment in bytes.
1202 // FIXME: Diagnose overflow.
1203 if (Pow2Alignment < 0)
Chris Lattner1fc3d752009-07-09 17:25:12 +00001204 return Error(Pow2AlignmentLoc, "invalid '.comm' or '.lcomm' directive "
1205 "alignment, can't be less than zero");
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001206
Daniel Dunbar8906ff12009-08-22 07:22:36 +00001207 if (!Sym->isUndefined())
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001208 return Error(IDLoc, "invalid symbol redefinition");
1209
Chris Lattner1fc3d752009-07-09 17:25:12 +00001210 // Create the Symbol as a common or local common with Size and Pow2Alignment
1211 Out.EmitCommonSymbol(Sym, Size, Pow2Alignment, IsLocal);
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001212
1213 return false;
1214}
Chris Lattner9be3fee2009-07-10 22:20:30 +00001215
1216/// ParseDirectiveDarwinZerofill
1217/// ::= .zerofill segname , sectname [, identifier , size_expression [
1218/// , align_expression ]]
1219bool AsmParser::ParseDirectiveDarwinZerofill() {
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00001220 // FIXME: Handle quoted names here.
1221
Daniel Dunbar3f872332009-07-28 16:08:33 +00001222 if (Lexer.isNot(AsmToken::Identifier))
Chris Lattner9be3fee2009-07-10 22:20:30 +00001223 return TokError("expected segment name after '.zerofill' directive");
Chris Lattnerff4bc462009-08-10 01:39:42 +00001224 StringRef Segment = Lexer.getTok().getString();
Chris Lattner9be3fee2009-07-10 22:20:30 +00001225 Lexer.Lex();
1226
Daniel Dunbar3f872332009-07-28 16:08:33 +00001227 if (Lexer.isNot(AsmToken::Comma))
Chris Lattner9be3fee2009-07-10 22:20:30 +00001228 return TokError("unexpected token in directive");
Chris Lattner9be3fee2009-07-10 22:20:30 +00001229 Lexer.Lex();
1230
Daniel Dunbar3f872332009-07-28 16:08:33 +00001231 if (Lexer.isNot(AsmToken::Identifier))
Chris Lattner9be3fee2009-07-10 22:20:30 +00001232 return TokError("expected section name after comma in '.zerofill' "
1233 "directive");
Chris Lattnerff4bc462009-08-10 01:39:42 +00001234 StringRef Section = Lexer.getTok().getString();
Chris Lattner9be3fee2009-07-10 22:20:30 +00001235 Lexer.Lex();
1236
Chris Lattner9be3fee2009-07-10 22:20:30 +00001237 // If this is the end of the line all that was wanted was to create the
1238 // the section but with no symbol.
Daniel Dunbar3f872332009-07-28 16:08:33 +00001239 if (Lexer.is(AsmToken::EndOfStatement)) {
Chris Lattnerff4bc462009-08-10 01:39:42 +00001240 // FIXME: CACHE THIS.
1241 MCSection *S = 0; //Ctx.GetSection(Section);
Chris Lattner6bdd74c2009-07-31 18:27:48 +00001242 if (S == 0)
Chris Lattnerff4bc462009-08-10 01:39:42 +00001243 S = MCSectionMachO::Create(Segment, Section,
1244 MCSectionMachO::S_ZEROFILL, 0,
1245 SectionKind(), Ctx);
Chris Lattner6bdd74c2009-07-31 18:27:48 +00001246
Chris Lattner9be3fee2009-07-10 22:20:30 +00001247 // Create the zerofill section but no symbol
Chris Lattner6bdd74c2009-07-31 18:27:48 +00001248 Out.EmitZerofill(S);
Chris Lattner9be3fee2009-07-10 22:20:30 +00001249 return false;
1250 }
1251
Daniel Dunbar3f872332009-07-28 16:08:33 +00001252 if (Lexer.isNot(AsmToken::Comma))
Chris Lattner9be3fee2009-07-10 22:20:30 +00001253 return TokError("unexpected token in directive");
1254 Lexer.Lex();
1255
Daniel Dunbar3f872332009-07-28 16:08:33 +00001256 if (Lexer.isNot(AsmToken::Identifier))
Chris Lattner9be3fee2009-07-10 22:20:30 +00001257 return TokError("expected identifier in directive");
1258
1259 // handle the identifier as the key symbol.
1260 SMLoc IDLoc = Lexer.getLoc();
Daniel Dunbar419aded2009-07-28 16:38:40 +00001261 MCSymbol *Sym = Ctx.GetOrCreateSymbol(Lexer.getTok().getString());
Chris Lattner9be3fee2009-07-10 22:20:30 +00001262 Lexer.Lex();
1263
Daniel Dunbar3f872332009-07-28 16:08:33 +00001264 if (Lexer.isNot(AsmToken::Comma))
Chris Lattner9be3fee2009-07-10 22:20:30 +00001265 return TokError("unexpected token in directive");
1266 Lexer.Lex();
1267
1268 int64_t Size;
1269 SMLoc SizeLoc = Lexer.getLoc();
1270 if (ParseAbsoluteExpression(Size))
1271 return true;
1272
1273 int64_t Pow2Alignment = 0;
1274 SMLoc Pow2AlignmentLoc;
Daniel Dunbar3f872332009-07-28 16:08:33 +00001275 if (Lexer.is(AsmToken::Comma)) {
Chris Lattner9be3fee2009-07-10 22:20:30 +00001276 Lexer.Lex();
1277 Pow2AlignmentLoc = Lexer.getLoc();
1278 if (ParseAbsoluteExpression(Pow2Alignment))
1279 return true;
1280 }
1281
Daniel Dunbar3f872332009-07-28 16:08:33 +00001282 if (Lexer.isNot(AsmToken::EndOfStatement))
Chris Lattner9be3fee2009-07-10 22:20:30 +00001283 return TokError("unexpected token in '.zerofill' directive");
1284
1285 Lexer.Lex();
1286
1287 if (Size < 0)
1288 return Error(SizeLoc, "invalid '.zerofill' directive size, can't be less "
1289 "than zero");
1290
1291 // NOTE: The alignment in the directive is a power of 2 value, the assember
1292 // may internally end up wanting an alignment in bytes.
1293 // FIXME: Diagnose overflow.
1294 if (Pow2Alignment < 0)
1295 return Error(Pow2AlignmentLoc, "invalid '.zerofill' directive alignment, "
1296 "can't be less than zero");
1297
Daniel Dunbar8906ff12009-08-22 07:22:36 +00001298 if (!Sym->isUndefined())
Chris Lattner9be3fee2009-07-10 22:20:30 +00001299 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