blob: 6eb30c76fedbe3b4e156590c6e1df51a7bde6a6c [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 Lattner56594f92009-07-31 17:47:16 +000020#include "llvm/MC/MCSection.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
86 return HadError;
Chris Lattnerb0789ed2009-06-21 20:54:55 +000087}
88
Kevin Enderbyc114ed72009-08-07 22:46:00 +000089/// ParseConditionalAssemblyDirectives - parse the conditional assembly
90/// directives
91bool AsmParser::ParseConditionalAssemblyDirectives(StringRef Directive,
92 SMLoc DirectiveLoc) {
93 if (Directive == ".if")
94 return ParseDirectiveIf(DirectiveLoc);
95 if (Directive == ".elseif")
96 return ParseDirectiveElseIf(DirectiveLoc);
97 if (Directive == ".else")
98 return ParseDirectiveElse(DirectiveLoc);
99 if (Directive == ".endif")
100 return ParseDirectiveEndIf(DirectiveLoc);
101 return true;
102}
103
Chris Lattner2cf5f142009-06-22 01:29:09 +0000104/// EatToEndOfStatement - Throw away the rest of the line for testing purposes.
105void AsmParser::EatToEndOfStatement() {
Daniel Dunbar3f872332009-07-28 16:08:33 +0000106 while (Lexer.isNot(AsmToken::EndOfStatement) &&
107 Lexer.isNot(AsmToken::Eof))
Chris Lattner2cf5f142009-06-22 01:29:09 +0000108 Lexer.Lex();
109
110 // Eat EOL.
Daniel Dunbar3f872332009-07-28 16:08:33 +0000111 if (Lexer.is(AsmToken::EndOfStatement))
Chris Lattner2cf5f142009-06-22 01:29:09 +0000112 Lexer.Lex();
113}
114
Chris Lattnerc4193832009-06-22 05:51:26 +0000115
Chris Lattner74ec1a32009-06-22 06:32:03 +0000116/// ParseParenExpr - Parse a paren expression and return it.
117/// NOTE: This assumes the leading '(' has already been consumed.
118///
119/// parenexpr ::= expr)
120///
Daniel Dunbar475839e2009-06-29 20:37:27 +0000121bool AsmParser::ParseParenExpr(AsmExpr *&Res) {
Chris Lattner74ec1a32009-06-22 06:32:03 +0000122 if (ParseExpression(Res)) return true;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000123 if (Lexer.isNot(AsmToken::RParen))
Chris Lattner74ec1a32009-06-22 06:32:03 +0000124 return TokError("expected ')' in parentheses expression");
125 Lexer.Lex();
126 return false;
127}
Chris Lattnerc4193832009-06-22 05:51:26 +0000128
Chris Lattner74ec1a32009-06-22 06:32:03 +0000129/// ParsePrimaryExpr - Parse a primary expression and return it.
130/// primaryexpr ::= (parenexpr
131/// primaryexpr ::= symbol
132/// primaryexpr ::= number
133/// primaryexpr ::= ~,+,- primaryexpr
Daniel Dunbar475839e2009-06-29 20:37:27 +0000134bool AsmParser::ParsePrimaryExpr(AsmExpr *&Res) {
Chris Lattnerc4193832009-06-22 05:51:26 +0000135 switch (Lexer.getKind()) {
136 default:
137 return TokError("unknown token in expression");
Daniel Dunbar3f872332009-07-28 16:08:33 +0000138 case AsmToken::Exclaim:
Daniel Dunbar475839e2009-06-29 20:37:27 +0000139 Lexer.Lex(); // Eat the operator.
140 if (ParsePrimaryExpr(Res))
141 return true;
142 Res = new AsmUnaryExpr(AsmUnaryExpr::LNot, Res);
143 return false;
Daniel Dunbar76c4d762009-07-31 21:55:09 +0000144 case AsmToken::String:
Daniel Dunbar3f872332009-07-28 16:08:33 +0000145 case AsmToken::Identifier: {
Chris Lattnerc4193832009-06-22 05:51:26 +0000146 // This is a label, this should be parsed as part of an expression, to
Daniel Dunbar475839e2009-06-29 20:37:27 +0000147 // handle things like LFOO+4.
Daniel Dunbar76c4d762009-07-31 21:55:09 +0000148 MCSymbol *Sym = Ctx.GetOrCreateSymbol(Lexer.getTok().getIdentifier());
Daniel Dunbardce0f3c2009-06-29 23:43:14 +0000149
150 // If this is use of an undefined symbol then mark it external.
151 if (!Sym->getSection() && !Ctx.GetSymbolValue(Sym))
152 Sym->setExternal(true);
153
154 Res = new AsmSymbolRefExpr(Sym);
Chris Lattnerc4193832009-06-22 05:51:26 +0000155 Lexer.Lex(); // Eat identifier.
156 return false;
Daniel Dunbardce0f3c2009-06-29 23:43:14 +0000157 }
Daniel Dunbar3f872332009-07-28 16:08:33 +0000158 case AsmToken::Integer:
Daniel Dunbar419aded2009-07-28 16:38:40 +0000159 Res = new AsmConstantExpr(Lexer.getTok().getIntVal());
Daniel Dunbar76c4d762009-07-31 21:55:09 +0000160 Lexer.Lex(); // Eat token.
Chris Lattnerc4193832009-06-22 05:51:26 +0000161 return false;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000162 case AsmToken::LParen:
Chris Lattner74ec1a32009-06-22 06:32:03 +0000163 Lexer.Lex(); // Eat the '('.
164 return ParseParenExpr(Res);
Daniel Dunbar3f872332009-07-28 16:08:33 +0000165 case AsmToken::Minus:
Chris Lattner74ec1a32009-06-22 06:32:03 +0000166 Lexer.Lex(); // Eat the operator.
Daniel Dunbar475839e2009-06-29 20:37:27 +0000167 if (ParsePrimaryExpr(Res))
168 return true;
169 Res = new AsmUnaryExpr(AsmUnaryExpr::Minus, Res);
170 return false;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000171 case AsmToken::Plus:
Daniel Dunbar475839e2009-06-29 20:37:27 +0000172 Lexer.Lex(); // Eat the operator.
173 if (ParsePrimaryExpr(Res))
174 return true;
175 Res = new AsmUnaryExpr(AsmUnaryExpr::Plus, Res);
176 return false;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000177 case AsmToken::Tilde:
Daniel Dunbar475839e2009-06-29 20:37:27 +0000178 Lexer.Lex(); // Eat the operator.
179 if (ParsePrimaryExpr(Res))
180 return true;
181 Res = new AsmUnaryExpr(AsmUnaryExpr::Not, Res);
182 return false;
Chris Lattnerc4193832009-06-22 05:51:26 +0000183 }
184}
Chris Lattner74ec1a32009-06-22 06:32:03 +0000185
186/// ParseExpression - Parse an expression and return it.
187///
188/// expr ::= expr +,- expr -> lowest.
189/// expr ::= expr |,^,&,! expr -> middle.
190/// expr ::= expr *,/,%,<<,>> expr -> highest.
191/// expr ::= primaryexpr
192///
Daniel Dunbar475839e2009-06-29 20:37:27 +0000193bool AsmParser::ParseExpression(AsmExpr *&Res) {
194 Res = 0;
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000195 return ParsePrimaryExpr(Res) ||
196 ParseBinOpRHS(1, Res);
Chris Lattner74ec1a32009-06-22 06:32:03 +0000197}
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000198
Daniel Dunbar475839e2009-06-29 20:37:27 +0000199bool AsmParser::ParseAbsoluteExpression(int64_t &Res) {
200 AsmExpr *Expr;
201
Daniel Dunbarf4b830f2009-06-30 02:10:03 +0000202 SMLoc StartLoc = Lexer.getLoc();
Daniel Dunbar475839e2009-06-29 20:37:27 +0000203 if (ParseExpression(Expr))
204 return true;
205
206 if (!Expr->EvaluateAsAbsolute(Ctx, Res))
Daniel Dunbarf4b830f2009-06-30 02:10:03 +0000207 return Error(StartLoc, "expected absolute expression");
Daniel Dunbar475839e2009-06-29 20:37:27 +0000208
209 return false;
210}
211
Daniel Dunbar15d17072009-06-30 01:49:52 +0000212bool AsmParser::ParseRelocatableExpression(MCValue &Res) {
213 AsmExpr *Expr;
214
Daniel Dunbarf4b830f2009-06-30 02:10:03 +0000215 SMLoc StartLoc = Lexer.getLoc();
Daniel Dunbar15d17072009-06-30 01:49:52 +0000216 if (ParseExpression(Expr))
217 return true;
218
219 if (!Expr->EvaluateAsRelocatable(Ctx, Res))
Daniel Dunbarf4b830f2009-06-30 02:10:03 +0000220 return Error(StartLoc, "expected relocatable expression");
Daniel Dunbar15d17072009-06-30 01:49:52 +0000221
222 return false;
223}
224
Daniel Dunbar2c3f00c2009-07-02 02:09:07 +0000225bool AsmParser::ParseParenRelocatableExpression(MCValue &Res) {
226 AsmExpr *Expr;
227
228 SMLoc StartLoc = Lexer.getLoc();
229 if (ParseParenExpr(Expr))
230 return true;
231
232 if (!Expr->EvaluateAsRelocatable(Ctx, Res))
233 return Error(StartLoc, "expected relocatable expression");
234
235 return false;
236}
237
Daniel Dunbar3f872332009-07-28 16:08:33 +0000238static unsigned getBinOpPrecedence(AsmToken::TokenKind K,
Daniel Dunbar475839e2009-06-29 20:37:27 +0000239 AsmBinaryExpr::Opcode &Kind) {
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000240 switch (K) {
241 default: return 0; // not a binop.
Daniel Dunbar475839e2009-06-29 20:37:27 +0000242
243 // Lowest Precedence: &&, ||
Daniel Dunbar3f872332009-07-28 16:08:33 +0000244 case AsmToken::AmpAmp:
Daniel Dunbar475839e2009-06-29 20:37:27 +0000245 Kind = AsmBinaryExpr::LAnd;
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000246 return 1;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000247 case AsmToken::PipePipe:
Daniel Dunbar475839e2009-06-29 20:37:27 +0000248 Kind = AsmBinaryExpr::LOr;
249 return 1;
250
251 // Low Precedence: +, -, ==, !=, <>, <, <=, >, >=
Daniel Dunbar3f872332009-07-28 16:08:33 +0000252 case AsmToken::Plus:
Daniel Dunbar475839e2009-06-29 20:37:27 +0000253 Kind = AsmBinaryExpr::Add;
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000254 return 2;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000255 case AsmToken::Minus:
Daniel Dunbar475839e2009-06-29 20:37:27 +0000256 Kind = AsmBinaryExpr::Sub;
257 return 2;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000258 case AsmToken::EqualEqual:
Daniel Dunbar475839e2009-06-29 20:37:27 +0000259 Kind = AsmBinaryExpr::EQ;
260 return 2;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000261 case AsmToken::ExclaimEqual:
262 case AsmToken::LessGreater:
Daniel Dunbar475839e2009-06-29 20:37:27 +0000263 Kind = AsmBinaryExpr::NE;
264 return 2;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000265 case AsmToken::Less:
Daniel Dunbar475839e2009-06-29 20:37:27 +0000266 Kind = AsmBinaryExpr::LT;
267 return 2;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000268 case AsmToken::LessEqual:
Daniel Dunbar475839e2009-06-29 20:37:27 +0000269 Kind = AsmBinaryExpr::LTE;
270 return 2;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000271 case AsmToken::Greater:
Daniel Dunbar475839e2009-06-29 20:37:27 +0000272 Kind = AsmBinaryExpr::GT;
273 return 2;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000274 case AsmToken::GreaterEqual:
Daniel Dunbar475839e2009-06-29 20:37:27 +0000275 Kind = AsmBinaryExpr::GTE;
276 return 2;
277
278 // Intermediate Precedence: |, &, ^
279 //
280 // FIXME: gas seems to support '!' as an infix operator?
Daniel Dunbar3f872332009-07-28 16:08:33 +0000281 case AsmToken::Pipe:
Daniel Dunbar475839e2009-06-29 20:37:27 +0000282 Kind = AsmBinaryExpr::Or;
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000283 return 3;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000284 case AsmToken::Caret:
Daniel Dunbar475839e2009-06-29 20:37:27 +0000285 Kind = AsmBinaryExpr::Xor;
286 return 3;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000287 case AsmToken::Amp:
Daniel Dunbar475839e2009-06-29 20:37:27 +0000288 Kind = AsmBinaryExpr::And;
289 return 3;
290
291 // Highest Precedence: *, /, %, <<, >>
Daniel Dunbar3f872332009-07-28 16:08:33 +0000292 case AsmToken::Star:
Daniel Dunbar475839e2009-06-29 20:37:27 +0000293 Kind = AsmBinaryExpr::Mul;
294 return 4;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000295 case AsmToken::Slash:
Daniel Dunbar475839e2009-06-29 20:37:27 +0000296 Kind = AsmBinaryExpr::Div;
297 return 4;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000298 case AsmToken::Percent:
Daniel Dunbar475839e2009-06-29 20:37:27 +0000299 Kind = AsmBinaryExpr::Mod;
300 return 4;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000301 case AsmToken::LessLess:
Daniel Dunbar475839e2009-06-29 20:37:27 +0000302 Kind = AsmBinaryExpr::Shl;
303 return 4;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000304 case AsmToken::GreaterGreater:
Daniel Dunbar475839e2009-06-29 20:37:27 +0000305 Kind = AsmBinaryExpr::Shr;
306 return 4;
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000307 }
308}
309
310
311/// ParseBinOpRHS - Parse all binary operators with precedence >= 'Precedence'.
312/// Res contains the LHS of the expression on input.
Daniel Dunbar475839e2009-06-29 20:37:27 +0000313bool AsmParser::ParseBinOpRHS(unsigned Precedence, AsmExpr *&Res) {
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000314 while (1) {
Daniel Dunbar51330632009-06-29 21:14:21 +0000315 AsmBinaryExpr::Opcode Kind = AsmBinaryExpr::Add;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000316 unsigned TokPrec = getBinOpPrecedence(Lexer.getKind(), Kind);
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000317
318 // If the next token is lower precedence than we are allowed to eat, return
319 // successfully with what we ate already.
320 if (TokPrec < Precedence)
321 return false;
322
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000323 Lexer.Lex();
324
325 // Eat the next primary expression.
Daniel Dunbar475839e2009-06-29 20:37:27 +0000326 AsmExpr *RHS;
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000327 if (ParsePrimaryExpr(RHS)) return true;
328
329 // If BinOp binds less tightly with RHS than the operator after RHS, let
330 // the pending operator take RHS as its LHS.
Daniel Dunbar475839e2009-06-29 20:37:27 +0000331 AsmBinaryExpr::Opcode Dummy;
332 unsigned NextTokPrec = getBinOpPrecedence(Lexer.getKind(), Dummy);
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000333 if (TokPrec < NextTokPrec) {
334 if (ParseBinOpRHS(Precedence+1, RHS)) return true;
335 }
336
Daniel Dunbar475839e2009-06-29 20:37:27 +0000337 // Merge LHS and RHS according to operator.
338 Res = new AsmBinaryExpr(Kind, Res, RHS);
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000339 }
340}
341
Chris Lattnerc4193832009-06-22 05:51:26 +0000342
343
344
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000345/// ParseStatement:
346/// ::= EndOfStatement
Chris Lattner2cf5f142009-06-22 01:29:09 +0000347/// ::= Label* Directive ...Operands... EndOfStatement
348/// ::= Label* Identifier OperandList* EndOfStatement
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000349bool AsmParser::ParseStatement() {
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +0000350 if (Lexer.is(AsmToken::EndOfStatement)) {
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000351 Lexer.Lex();
352 return false;
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000353 }
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +0000354
355 // Statements always start with an identifier.
Daniel Dunbar419aded2009-07-28 16:38:40 +0000356 AsmToken ID = Lexer.getTok();
357 SMLoc IDLoc = ID.getLoc();
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +0000358 StringRef IDVal;
359 if (ParseIdentifier(IDVal))
360 return TokError("unexpected token at start of statement");
361
362 // FIXME: Recurse on local labels?
363
364 // See what kind of statement we have.
365 switch (Lexer.getKind()) {
Daniel Dunbar3f872332009-07-28 16:08:33 +0000366 case AsmToken::Colon: {
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000367 // identifier ':' -> Label.
368 Lexer.Lex();
Daniel Dunbardce0f3c2009-06-29 23:43:14 +0000369
370 // Diagnose attempt to use a variable as a label.
371 //
372 // FIXME: Diagnostics. Note the location of the definition as a label.
373 // FIXME: This doesn't diagnose assignment to a symbol which has been
374 // implicitly marked as external.
375 MCSymbol *Sym = Ctx.GetOrCreateSymbol(IDVal);
376 if (Sym->getSection())
377 return Error(IDLoc, "invalid symbol redefinition");
378 if (Ctx.GetSymbolValue(Sym))
379 return Error(IDLoc, "symbol already used as assembler variable");
Chris Lattnerc69485e2009-06-24 04:31:49 +0000380
381 // Since we saw a label, create a symbol and emit it.
382 // FIXME: If the label starts with L it is an assembler temporary label.
383 // Why does the client of this api need to know this?
Daniel Dunbardce0f3c2009-06-29 23:43:14 +0000384 Out.EmitLabel(Sym);
385
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000386 return ParseStatement();
Daniel Dunbardce0f3c2009-06-29 23:43:14 +0000387 }
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000388
Daniel Dunbar3f872332009-07-28 16:08:33 +0000389 case AsmToken::Equal:
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000390 // identifier '=' ... -> assignment statement
391 Lexer.Lex();
392
393 return ParseAssignment(IDVal, false);
394
395 default: // Normal instruction or directive.
396 break;
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000397 }
398
399 // Otherwise, we have a normal instruction or directive.
Chris Lattner2cf5f142009-06-22 01:29:09 +0000400 if (IDVal[0] == '.') {
Chris Lattner529fb542009-06-24 05:13:15 +0000401 // FIXME: This should be driven based on a hash lookup and callback.
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000402 if (IDVal == ".section")
Chris Lattner529fb542009-06-24 05:13:15 +0000403 return ParseDirectiveDarwinSection();
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000404 if (IDVal == ".text")
Chris Lattner529fb542009-06-24 05:13:15 +0000405 // FIXME: This changes behavior based on the -static flag to the
406 // assembler.
Chris Lattnerff4bc462009-08-10 01:39:42 +0000407 return ParseDirectiveSectionSwitch("__TEXT", "__text",
408 MCSectionMachO::S_ATTR_PURE_INSTRUCTIONS);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000409 if (IDVal == ".const")
Chris Lattnerff4bc462009-08-10 01:39:42 +0000410 return ParseDirectiveSectionSwitch("__TEXT", "__const", 0);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000411 if (IDVal == ".static_const")
Chris Lattnerff4bc462009-08-10 01:39:42 +0000412 return ParseDirectiveSectionSwitch("__TEXT", "__static_const", 0);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000413 if (IDVal == ".cstring")
Chris Lattnerff4bc462009-08-10 01:39:42 +0000414 return ParseDirectiveSectionSwitch("__TEXT","__cstring",
415 MCSectionMachO::S_CSTRING_LITERALS);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000416 if (IDVal == ".literal4")
Chris Lattnerff4bc462009-08-10 01:39:42 +0000417 return ParseDirectiveSectionSwitch("__TEXT", "__literal4",
418 MCSectionMachO::S_4BYTE_LITERALS);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000419 if (IDVal == ".literal8")
Chris Lattnerff4bc462009-08-10 01:39:42 +0000420 return ParseDirectiveSectionSwitch("__TEXT", "__literal8",
421 MCSectionMachO::S_8BYTE_LITERALS);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000422 if (IDVal == ".literal16")
Chris Lattnerff4bc462009-08-10 01:39:42 +0000423 return ParseDirectiveSectionSwitch("__TEXT","__literal16",
424 MCSectionMachO::S_16BYTE_LITERALS);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000425 if (IDVal == ".constructor")
Chris Lattnerff4bc462009-08-10 01:39:42 +0000426 return ParseDirectiveSectionSwitch("__TEXT","__constructor", 0);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000427 if (IDVal == ".destructor")
Chris Lattnerff4bc462009-08-10 01:39:42 +0000428 return ParseDirectiveSectionSwitch("__TEXT","__destructor", 0);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000429 if (IDVal == ".fvmlib_init0")
Chris Lattnerff4bc462009-08-10 01:39:42 +0000430 return ParseDirectiveSectionSwitch("__TEXT","__fvmlib_init0", 0);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000431 if (IDVal == ".fvmlib_init1")
Chris Lattnerff4bc462009-08-10 01:39:42 +0000432 return ParseDirectiveSectionSwitch("__TEXT","__fvmlib_init1", 0);
433 if (IDVal == ".symbol_stub")
434 return ParseDirectiveSectionSwitch("__TEXT","__symbol_stub",
435 MCSectionMachO::S_SYMBOL_STUBS |
436 MCSectionMachO::S_ATTR_SELF_MODIFYING_CODE |
437 MCSectionMachO::S_ATTR_PURE_INSTRUCTIONS,
438 // FIXME: Different on PPC and ARM.
439 16);
Chris Lattner529fb542009-06-24 05:13:15 +0000440 // FIXME: .picsymbol_stub on PPC.
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000441 if (IDVal == ".data")
Chris Lattnerff4bc462009-08-10 01:39:42 +0000442 return ParseDirectiveSectionSwitch("__DATA", "__data", 0);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000443 if (IDVal == ".static_data")
Chris Lattnerff4bc462009-08-10 01:39:42 +0000444 return ParseDirectiveSectionSwitch("__DATA", "__static_data", 0);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000445 if (IDVal == ".non_lazy_symbol_pointer")
Chris Lattnerff4bc462009-08-10 01:39:42 +0000446 return ParseDirectiveSectionSwitch("__DATA", "__nl_symbol_pointer",
447 MCSectionMachO::S_NON_LAZY_SYMBOL_POINTERS);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000448 if (IDVal == ".lazy_symbol_pointer")
Chris Lattnerff4bc462009-08-10 01:39:42 +0000449 return ParseDirectiveSectionSwitch("__DATA", "__la_symbol_pointer",
450 MCSectionMachO::S_LAZY_SYMBOL_POINTERS);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000451 if (IDVal == ".dyld")
Chris Lattnerff4bc462009-08-10 01:39:42 +0000452 return ParseDirectiveSectionSwitch("__DATA", "__dyld", 0);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000453 if (IDVal == ".mod_init_func")
Chris Lattnerff4bc462009-08-10 01:39:42 +0000454 return ParseDirectiveSectionSwitch("__DATA", "__mod_init_func",
455 MCSectionMachO::S_MOD_INIT_FUNC_POINTERS);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000456 if (IDVal == ".mod_term_func")
Chris Lattnerff4bc462009-08-10 01:39:42 +0000457 return ParseDirectiveSectionSwitch("__DATA", "__mod_term_func",
458 MCSectionMachO::S_MOD_TERM_FUNC_POINTERS);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000459 if (IDVal == ".const_data")
Chris Lattnerff4bc462009-08-10 01:39:42 +0000460 return ParseDirectiveSectionSwitch("__DATA", "__const", 0);
Chris Lattner529fb542009-06-24 05:13:15 +0000461
462
463 // FIXME: Verify attributes on sections.
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000464 if (IDVal == ".objc_class")
Chris Lattnerff4bc462009-08-10 01:39:42 +0000465 return ParseDirectiveSectionSwitch("__OBJC", "__class", 0);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000466 if (IDVal == ".objc_meta_class")
Chris Lattnerff4bc462009-08-10 01:39:42 +0000467 return ParseDirectiveSectionSwitch("__OBJC", "__meta_class", 0);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000468 if (IDVal == ".objc_cat_cls_meth")
Chris Lattnerff4bc462009-08-10 01:39:42 +0000469 return ParseDirectiveSectionSwitch("__OBJC", "__cat_cls_meth", 0);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000470 if (IDVal == ".objc_cat_inst_meth")
Chris Lattnerff4bc462009-08-10 01:39:42 +0000471 return ParseDirectiveSectionSwitch("__OBJC", "__cat_inst_meth", 0);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000472 if (IDVal == ".objc_protocol")
Chris Lattnerff4bc462009-08-10 01:39:42 +0000473 return ParseDirectiveSectionSwitch("__OBJC", "__protocol", 0);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000474 if (IDVal == ".objc_string_object")
Chris Lattnerff4bc462009-08-10 01:39:42 +0000475 return ParseDirectiveSectionSwitch("__OBJC", "__string_object", 0);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000476 if (IDVal == ".objc_cls_meth")
Chris Lattnerff4bc462009-08-10 01:39:42 +0000477 return ParseDirectiveSectionSwitch("__OBJC", "__cls_meth", 0);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000478 if (IDVal == ".objc_inst_meth")
Chris Lattnerff4bc462009-08-10 01:39:42 +0000479 return ParseDirectiveSectionSwitch("__OBJC", "__inst_meth", 0);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000480 if (IDVal == ".objc_cls_refs")
Chris Lattnerff4bc462009-08-10 01:39:42 +0000481 return ParseDirectiveSectionSwitch("__OBJC", "__cls_refs", 0);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000482 if (IDVal == ".objc_message_refs")
Chris Lattnerff4bc462009-08-10 01:39:42 +0000483 return ParseDirectiveSectionSwitch("__OBJC", "__message_refs", 0);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000484 if (IDVal == ".objc_symbols")
Chris Lattnerff4bc462009-08-10 01:39:42 +0000485 return ParseDirectiveSectionSwitch("__OBJC", "__symbols", 0);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000486 if (IDVal == ".objc_category")
Chris Lattnerff4bc462009-08-10 01:39:42 +0000487 return ParseDirectiveSectionSwitch("__OBJC", "__category", 0);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000488 if (IDVal == ".objc_class_vars")
Chris Lattnerff4bc462009-08-10 01:39:42 +0000489 return ParseDirectiveSectionSwitch("__OBJC", "__class_vars", 0);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000490 if (IDVal == ".objc_instance_vars")
Chris Lattnerff4bc462009-08-10 01:39:42 +0000491 return ParseDirectiveSectionSwitch("__OBJC", "__instance_vars", 0);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000492 if (IDVal == ".objc_module_info")
Chris Lattnerff4bc462009-08-10 01:39:42 +0000493 return ParseDirectiveSectionSwitch("__OBJC", "__module_info", 0);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000494 if (IDVal == ".objc_class_names")
Chris Lattnerff4bc462009-08-10 01:39:42 +0000495 return ParseDirectiveSectionSwitch("__TEXT", "__cstring",
496 MCSectionMachO::S_CSTRING_LITERALS);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000497 if (IDVal == ".objc_meth_var_types")
Chris Lattnerff4bc462009-08-10 01:39:42 +0000498 return ParseDirectiveSectionSwitch("__TEXT", "__cstring",
499 MCSectionMachO::S_CSTRING_LITERALS);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000500 if (IDVal == ".objc_meth_var_names")
Chris Lattnerff4bc462009-08-10 01:39:42 +0000501 return ParseDirectiveSectionSwitch("__TEXT", "__cstring",
502 MCSectionMachO::S_CSTRING_LITERALS);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000503 if (IDVal == ".objc_selector_strs")
Chris Lattnerff4bc462009-08-10 01:39:42 +0000504 return ParseDirectiveSectionSwitch("__OBJC", "__selector_strs", 0);
Chris Lattner9a023f72009-06-24 04:43:34 +0000505
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000506 // Assembler features
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000507 if (IDVal == ".set")
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000508 return ParseDirectiveSet();
509
Daniel Dunbara0d14262009-06-24 23:30:00 +0000510 // Data directives
511
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000512 if (IDVal == ".ascii")
Daniel Dunbara0d14262009-06-24 23:30:00 +0000513 return ParseDirectiveAscii(false);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000514 if (IDVal == ".asciz")
Daniel Dunbara0d14262009-06-24 23:30:00 +0000515 return ParseDirectiveAscii(true);
516
517 // FIXME: Target hooks for size? Also for "word", "hword".
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000518 if (IDVal == ".byte")
Daniel Dunbara0d14262009-06-24 23:30:00 +0000519 return ParseDirectiveValue(1);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000520 if (IDVal == ".short")
Daniel Dunbara0d14262009-06-24 23:30:00 +0000521 return ParseDirectiveValue(2);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000522 if (IDVal == ".long")
Daniel Dunbara0d14262009-06-24 23:30:00 +0000523 return ParseDirectiveValue(4);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000524 if (IDVal == ".quad")
Daniel Dunbara0d14262009-06-24 23:30:00 +0000525 return ParseDirectiveValue(8);
Daniel Dunbarc29dfa72009-06-29 23:46:59 +0000526
527 // FIXME: Target hooks for IsPow2.
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000528 if (IDVal == ".align")
Daniel Dunbarc29dfa72009-06-29 23:46:59 +0000529 return ParseDirectiveAlign(/*IsPow2=*/true, /*ExprSize=*/1);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000530 if (IDVal == ".align32")
Daniel Dunbarc29dfa72009-06-29 23:46:59 +0000531 return ParseDirectiveAlign(/*IsPow2=*/true, /*ExprSize=*/4);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000532 if (IDVal == ".balign")
Daniel Dunbarc29dfa72009-06-29 23:46:59 +0000533 return ParseDirectiveAlign(/*IsPow2=*/false, /*ExprSize=*/1);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000534 if (IDVal == ".balignw")
Daniel Dunbarc29dfa72009-06-29 23:46:59 +0000535 return ParseDirectiveAlign(/*IsPow2=*/false, /*ExprSize=*/2);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000536 if (IDVal == ".balignl")
Daniel Dunbarc29dfa72009-06-29 23:46:59 +0000537 return ParseDirectiveAlign(/*IsPow2=*/false, /*ExprSize=*/4);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000538 if (IDVal == ".p2align")
Daniel Dunbarc29dfa72009-06-29 23:46:59 +0000539 return ParseDirectiveAlign(/*IsPow2=*/true, /*ExprSize=*/1);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000540 if (IDVal == ".p2alignw")
Daniel Dunbarc29dfa72009-06-29 23:46:59 +0000541 return ParseDirectiveAlign(/*IsPow2=*/true, /*ExprSize=*/2);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000542 if (IDVal == ".p2alignl")
Daniel Dunbarc29dfa72009-06-29 23:46:59 +0000543 return ParseDirectiveAlign(/*IsPow2=*/true, /*ExprSize=*/4);
544
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000545 if (IDVal == ".org")
Daniel Dunbarc238b582009-06-25 22:44:51 +0000546 return ParseDirectiveOrg();
Daniel Dunbarc29dfa72009-06-29 23:46:59 +0000547
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000548 if (IDVal == ".fill")
Daniel Dunbarc29dfa72009-06-29 23:46:59 +0000549 return ParseDirectiveFill();
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000550 if (IDVal == ".space")
Daniel Dunbara0d14262009-06-24 23:30:00 +0000551 return ParseDirectiveSpace();
552
Daniel Dunbard7b267b2009-06-30 00:33:19 +0000553 // Symbol attribute directives
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000554 if (IDVal == ".globl" || IDVal == ".global")
Daniel Dunbard7b267b2009-06-30 00:33:19 +0000555 return ParseDirectiveSymbolAttribute(MCStreamer::Global);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000556 if (IDVal == ".hidden")
Daniel Dunbard7b267b2009-06-30 00:33:19 +0000557 return ParseDirectiveSymbolAttribute(MCStreamer::Hidden);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000558 if (IDVal == ".indirect_symbol")
Daniel Dunbard7b267b2009-06-30 00:33:19 +0000559 return ParseDirectiveSymbolAttribute(MCStreamer::IndirectSymbol);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000560 if (IDVal == ".internal")
Daniel Dunbard7b267b2009-06-30 00:33:19 +0000561 return ParseDirectiveSymbolAttribute(MCStreamer::Internal);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000562 if (IDVal == ".lazy_reference")
Daniel Dunbard7b267b2009-06-30 00:33:19 +0000563 return ParseDirectiveSymbolAttribute(MCStreamer::LazyReference);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000564 if (IDVal == ".no_dead_strip")
Daniel Dunbard7b267b2009-06-30 00:33:19 +0000565 return ParseDirectiveSymbolAttribute(MCStreamer::NoDeadStrip);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000566 if (IDVal == ".private_extern")
Daniel Dunbard7b267b2009-06-30 00:33:19 +0000567 return ParseDirectiveSymbolAttribute(MCStreamer::PrivateExtern);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000568 if (IDVal == ".protected")
Daniel Dunbard7b267b2009-06-30 00:33:19 +0000569 return ParseDirectiveSymbolAttribute(MCStreamer::Protected);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000570 if (IDVal == ".reference")
Daniel Dunbard7b267b2009-06-30 00:33:19 +0000571 return ParseDirectiveSymbolAttribute(MCStreamer::Reference);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000572 if (IDVal == ".weak")
Daniel Dunbard7b267b2009-06-30 00:33:19 +0000573 return ParseDirectiveSymbolAttribute(MCStreamer::Weak);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000574 if (IDVal == ".weak_definition")
Daniel Dunbard7b267b2009-06-30 00:33:19 +0000575 return ParseDirectiveSymbolAttribute(MCStreamer::WeakDefinition);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000576 if (IDVal == ".weak_reference")
Daniel Dunbard7b267b2009-06-30 00:33:19 +0000577 return ParseDirectiveSymbolAttribute(MCStreamer::WeakReference);
578
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000579 if (IDVal == ".comm")
Chris Lattner1fc3d752009-07-09 17:25:12 +0000580 return ParseDirectiveComm(/*IsLocal=*/false);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000581 if (IDVal == ".lcomm")
Chris Lattner1fc3d752009-07-09 17:25:12 +0000582 return ParseDirectiveComm(/*IsLocal=*/true);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000583 if (IDVal == ".zerofill")
Chris Lattner9be3fee2009-07-10 22:20:30 +0000584 return ParseDirectiveDarwinZerofill();
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000585 if (IDVal == ".desc")
Kevin Enderby95cf30c2009-07-14 18:17:10 +0000586 return ParseDirectiveDarwinSymbolDesc();
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000587 if (IDVal == ".lsym")
Kevin Enderby71148242009-07-14 21:35:03 +0000588 return ParseDirectiveDarwinLsym();
Chris Lattner4e4db7a2009-07-07 20:30:46 +0000589
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000590 if (IDVal == ".subsections_via_symbols")
Kevin Enderbya5c78322009-07-13 21:03:15 +0000591 return ParseDirectiveDarwinSubsectionsViaSymbols();
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000592 if (IDVal == ".abort")
Kevin Enderby5f1f0b82009-07-13 23:15:14 +0000593 return ParseDirectiveAbort();
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000594 if (IDVal == ".include")
Kevin Enderby1f049b22009-07-14 23:21:55 +0000595 return ParseDirectiveInclude();
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000596 if (IDVal == ".dump")
Kevin Enderby5026ae42009-07-20 20:25:37 +0000597 return ParseDirectiveDarwinDumpOrLoad(IDLoc, /*IsDump=*/true);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000598 if (IDVal == ".load")
Kevin Enderby5026ae42009-07-20 20:25:37 +0000599 return ParseDirectiveDarwinDumpOrLoad(IDLoc, /*IsLoad=*/false);
Kevin Enderbya5c78322009-07-13 21:03:15 +0000600
Daniel Dunbar3fb76832009-06-30 00:49:23 +0000601 Warning(IDLoc, "ignoring directive for now");
Chris Lattner2cf5f142009-06-22 01:29:09 +0000602 EatToEndOfStatement();
603 return false;
604 }
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000605
Chris Lattner29dfe7c2009-06-23 18:41:30 +0000606 MCInst Inst;
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000607 if (getTargetParser().ParseInstruction(IDVal, Inst))
Chris Lattner29dfe7c2009-06-23 18:41:30 +0000608 return true;
Chris Lattner2cf5f142009-06-22 01:29:09 +0000609
Daniel Dunbar3f872332009-07-28 16:08:33 +0000610 if (Lexer.isNot(AsmToken::EndOfStatement))
Chris Lattner9a023f72009-06-24 04:43:34 +0000611 return TokError("unexpected token in argument list");
Chris Lattner2cf5f142009-06-22 01:29:09 +0000612
613 // Eat the end of statement marker.
614 Lexer.Lex();
615
616 // Instruction is good, process it.
Daniel Dunbar0eebb052009-07-01 06:35:48 +0000617 Out.EmitInstruction(Inst);
Chris Lattner2cf5f142009-06-22 01:29:09 +0000618
619 // Skip to end of line for now.
Chris Lattner27aa7d22009-06-21 20:16:42 +0000620 return false;
621}
Chris Lattner9a023f72009-06-24 04:43:34 +0000622
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000623bool AsmParser::ParseAssignment(const StringRef &Name, bool IsDotSet) {
Daniel Dunbardce0f3c2009-06-29 23:43:14 +0000624 // FIXME: Use better location, we should use proper tokens.
625 SMLoc EqualLoc = Lexer.getLoc();
626
Daniel Dunbarf4b830f2009-06-30 02:10:03 +0000627 MCValue Value;
628 if (ParseRelocatableExpression(Value))
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000629 return true;
630
Daniel Dunbar3f872332009-07-28 16:08:33 +0000631 if (Lexer.isNot(AsmToken::EndOfStatement))
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000632 return TokError("unexpected token in assignment");
633
634 // Eat the end of statement marker.
635 Lexer.Lex();
636
Daniel Dunbardce0f3c2009-06-29 23:43:14 +0000637 // Diagnose assignment to a label.
638 //
639 // FIXME: Diagnostics. Note the location of the definition as a label.
640 // FIXME: This doesn't diagnose assignment to a symbol which has been
641 // implicitly marked as external.
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000642 // FIXME: Handle '.'.
Daniel Dunbardce0f3c2009-06-29 23:43:14 +0000643 // FIXME: Diagnose assignment to protected identifier (e.g., register name).
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000644 MCSymbol *Sym = Ctx.GetOrCreateSymbol(Name);
Daniel Dunbardce0f3c2009-06-29 23:43:14 +0000645 if (Sym->getSection())
646 return Error(EqualLoc, "invalid assignment to symbol emitted as a label");
647 if (Sym->isExternal())
648 return Error(EqualLoc, "invalid assignment to external symbol");
649
650 // Do the assignment.
Daniel Dunbarf4b830f2009-06-30 02:10:03 +0000651 Out.EmitAssignment(Sym, Value, IsDotSet);
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000652
653 return false;
654}
655
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +0000656/// ParseIdentifier:
657/// ::= identifier
658/// ::= string
659bool AsmParser::ParseIdentifier(StringRef &Res) {
660 if (Lexer.isNot(AsmToken::Identifier) &&
661 Lexer.isNot(AsmToken::String))
662 return true;
663
664 Res = Lexer.getTok().getIdentifier();
665
666 Lexer.Lex(); // Consume the identifier token.
667
668 return false;
669}
670
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000671/// ParseDirectiveSet:
672/// ::= .set identifier ',' expression
673bool AsmParser::ParseDirectiveSet() {
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +0000674 StringRef Name;
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000675
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +0000676 if (ParseIdentifier(Name))
677 return TokError("expected identifier after '.set' directive");
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000678
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +0000679 if (Lexer.isNot(AsmToken::Comma))
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000680 return TokError("unexpected token in '.set'");
681 Lexer.Lex();
682
683 return ParseAssignment(Name, true);
684}
685
Chris Lattner9a023f72009-06-24 04:43:34 +0000686/// ParseDirectiveSection:
Chris Lattner529fb542009-06-24 05:13:15 +0000687/// ::= .section identifier (',' identifier)*
688/// FIXME: This should actually parse out the segment, section, attributes and
689/// sizeof_stub fields.
690bool AsmParser::ParseDirectiveDarwinSection() {
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +0000691 StringRef SectionName;
692
Chris Lattnerff4bc462009-08-10 01:39:42 +0000693 if (Lexer.isNot(AsmToken::Identifier))
Chris Lattner9a023f72009-06-24 04:43:34 +0000694 return TokError("expected identifier after '.section' directive");
695
Chris Lattnerff4bc462009-08-10 01:39:42 +0000696 std::string SectionSpec = SectionName;
697 StringRef EOL = Lexer.LexUntilEndOfStatement();
698 SectionSpec.append(EOL.begin(), EOL.end());
699 Lexer.Lex();
Daniel Dunbar3f872332009-07-28 16:08:33 +0000700 if (Lexer.isNot(AsmToken::EndOfStatement))
Chris Lattner9a023f72009-06-24 04:43:34 +0000701 return TokError("unexpected token in '.section' directive");
702 Lexer.Lex();
703
Chris Lattnerff4bc462009-08-10 01:39:42 +0000704
705 StringRef Segment, Section;
706 unsigned TAA, StubSize;
707 std::string ErrorStr =
708 MCSectionMachO::ParseSectionSpecifier(SectionSpec, Segment, Section,
709 TAA, StubSize);
710
711 if (!ErrorStr.empty())
712 return TokError(ErrorStr.c_str());
713
714 // FIXME: CACHE THESE.
715
Chris Lattner56594f92009-07-31 17:47:16 +0000716 // FIXME: Arch specific.
Chris Lattnerff4bc462009-08-10 01:39:42 +0000717 MCSection *S = 0; //Ctx.GetSection(Section);
Chris Lattner56594f92009-07-31 17:47:16 +0000718 if (S == 0)
Chris Lattnerff4bc462009-08-10 01:39:42 +0000719 S = MCSectionMachO::Create(Segment, Section, TAA, StubSize,
720 SectionKind(), Ctx);
Chris Lattner56594f92009-07-31 17:47:16 +0000721
722 Out.SwitchSection(S);
Chris Lattner9a023f72009-06-24 04:43:34 +0000723 return false;
724}
725
Chris Lattnere15c2d72009-08-10 18:05:55 +0000726/// ParseDirectiveSectionSwitch -
727///
728/// FIXME! Many of these directives implicitly cause a ".align" directive to get
729/// emitted, we don't do this yet which can lead to subtle miscompiles.
Chris Lattnerff4bc462009-08-10 01:39:42 +0000730bool AsmParser::ParseDirectiveSectionSwitch(const char *Segment,
731 const char *Section,
732 unsigned TAA, unsigned StubSize) {
Daniel Dunbar3f872332009-07-28 16:08:33 +0000733 if (Lexer.isNot(AsmToken::EndOfStatement))
Chris Lattner529fb542009-06-24 05:13:15 +0000734 return TokError("unexpected token in section switching directive");
735 Lexer.Lex();
736
Chris Lattner56594f92009-07-31 17:47:16 +0000737 // FIXME: Arch specific.
Chris Lattnerff4bc462009-08-10 01:39:42 +0000738 // FIXME: Cache this!
739 MCSection *S = 0; // Ctx.GetSection(Section);
Chris Lattner56594f92009-07-31 17:47:16 +0000740 if (S == 0)
Chris Lattnerff4bc462009-08-10 01:39:42 +0000741 S = MCSectionMachO::Create(Segment, Section, TAA, StubSize,
742 SectionKind(), Ctx);
Chris Lattner56594f92009-07-31 17:47:16 +0000743
744 Out.SwitchSection(S);
Chris Lattner529fb542009-06-24 05:13:15 +0000745 return false;
746}
Daniel Dunbara0d14262009-06-24 23:30:00 +0000747
748/// ParseDirectiveAscii:
Daniel Dunbar475839e2009-06-29 20:37:27 +0000749/// ::= ( .ascii | .asciz ) [ "string" ( , "string" )* ]
Daniel Dunbara0d14262009-06-24 23:30:00 +0000750bool AsmParser::ParseDirectiveAscii(bool ZeroTerminated) {
Daniel Dunbar3f872332009-07-28 16:08:33 +0000751 if (Lexer.isNot(AsmToken::EndOfStatement)) {
Daniel Dunbara0d14262009-06-24 23:30:00 +0000752 for (;;) {
Daniel Dunbar3f872332009-07-28 16:08:33 +0000753 if (Lexer.isNot(AsmToken::String))
Daniel Dunbara0d14262009-06-24 23:30:00 +0000754 return TokError("expected string in '.ascii' or '.asciz' directive");
755
756 // FIXME: This shouldn't use a const char* + strlen, the string could have
757 // embedded nulls.
758 // FIXME: Should have accessor for getting string contents.
Daniel Dunbar419aded2009-07-28 16:38:40 +0000759 StringRef Str = Lexer.getTok().getString();
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000760 Out.EmitBytes(Str.substr(1, Str.size() - 2));
Daniel Dunbara0d14262009-06-24 23:30:00 +0000761 if (ZeroTerminated)
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000762 Out.EmitBytes(StringRef("\0", 1));
Daniel Dunbara0d14262009-06-24 23:30:00 +0000763
764 Lexer.Lex();
765
Daniel Dunbar3f872332009-07-28 16:08:33 +0000766 if (Lexer.is(AsmToken::EndOfStatement))
Daniel Dunbara0d14262009-06-24 23:30:00 +0000767 break;
768
Daniel Dunbar3f872332009-07-28 16:08:33 +0000769 if (Lexer.isNot(AsmToken::Comma))
Daniel Dunbara0d14262009-06-24 23:30:00 +0000770 return TokError("unexpected token in '.ascii' or '.asciz' directive");
771 Lexer.Lex();
772 }
773 }
774
775 Lexer.Lex();
776 return false;
777}
778
779/// ParseDirectiveValue
780/// ::= (.byte | .short | ... ) [ expression (, expression)* ]
781bool AsmParser::ParseDirectiveValue(unsigned Size) {
Daniel Dunbar3f872332009-07-28 16:08:33 +0000782 if (Lexer.isNot(AsmToken::EndOfStatement)) {
Daniel Dunbara0d14262009-06-24 23:30:00 +0000783 for (;;) {
Daniel Dunbarf4b830f2009-06-30 02:10:03 +0000784 MCValue Expr;
785 if (ParseRelocatableExpression(Expr))
Daniel Dunbara0d14262009-06-24 23:30:00 +0000786 return true;
787
Daniel Dunbarf4b830f2009-06-30 02:10:03 +0000788 Out.EmitValue(Expr, Size);
Daniel Dunbara0d14262009-06-24 23:30:00 +0000789
Daniel Dunbar3f872332009-07-28 16:08:33 +0000790 if (Lexer.is(AsmToken::EndOfStatement))
Daniel Dunbara0d14262009-06-24 23:30:00 +0000791 break;
792
793 // FIXME: Improve diagnostic.
Daniel Dunbar3f872332009-07-28 16:08:33 +0000794 if (Lexer.isNot(AsmToken::Comma))
Daniel Dunbara0d14262009-06-24 23:30:00 +0000795 return TokError("unexpected token in directive");
796 Lexer.Lex();
797 }
798 }
799
800 Lexer.Lex();
801 return false;
802}
803
804/// ParseDirectiveSpace
805/// ::= .space expression [ , expression ]
806bool AsmParser::ParseDirectiveSpace() {
807 int64_t NumBytes;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000808 if (ParseAbsoluteExpression(NumBytes))
Daniel Dunbara0d14262009-06-24 23:30:00 +0000809 return true;
810
811 int64_t FillExpr = 0;
812 bool HasFillExpr = false;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000813 if (Lexer.isNot(AsmToken::EndOfStatement)) {
814 if (Lexer.isNot(AsmToken::Comma))
Daniel Dunbara0d14262009-06-24 23:30:00 +0000815 return TokError("unexpected token in '.space' directive");
816 Lexer.Lex();
817
Daniel Dunbar475839e2009-06-29 20:37:27 +0000818 if (ParseAbsoluteExpression(FillExpr))
Daniel Dunbara0d14262009-06-24 23:30:00 +0000819 return true;
820
821 HasFillExpr = true;
822
Daniel Dunbar3f872332009-07-28 16:08:33 +0000823 if (Lexer.isNot(AsmToken::EndOfStatement))
Daniel Dunbara0d14262009-06-24 23:30:00 +0000824 return TokError("unexpected token in '.space' directive");
825 }
826
827 Lexer.Lex();
828
829 if (NumBytes <= 0)
830 return TokError("invalid number of bytes in '.space' directive");
831
832 // FIXME: Sometimes the fill expr is 'nop' if it isn't supplied, instead of 0.
833 for (uint64_t i = 0, e = NumBytes; i != e; ++i)
834 Out.EmitValue(MCValue::get(FillExpr), 1);
835
836 return false;
837}
838
839/// ParseDirectiveFill
840/// ::= .fill expression , expression , expression
841bool AsmParser::ParseDirectiveFill() {
842 int64_t NumValues;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000843 if (ParseAbsoluteExpression(NumValues))
Daniel Dunbara0d14262009-06-24 23:30:00 +0000844 return true;
845
Daniel Dunbar3f872332009-07-28 16:08:33 +0000846 if (Lexer.isNot(AsmToken::Comma))
Daniel Dunbara0d14262009-06-24 23:30:00 +0000847 return TokError("unexpected token in '.fill' directive");
848 Lexer.Lex();
849
850 int64_t FillSize;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000851 if (ParseAbsoluteExpression(FillSize))
Daniel Dunbara0d14262009-06-24 23:30:00 +0000852 return true;
853
Daniel Dunbar3f872332009-07-28 16:08:33 +0000854 if (Lexer.isNot(AsmToken::Comma))
Daniel Dunbara0d14262009-06-24 23:30:00 +0000855 return TokError("unexpected token in '.fill' directive");
856 Lexer.Lex();
857
858 int64_t FillExpr;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000859 if (ParseAbsoluteExpression(FillExpr))
Daniel Dunbara0d14262009-06-24 23:30:00 +0000860 return true;
861
Daniel Dunbar3f872332009-07-28 16:08:33 +0000862 if (Lexer.isNot(AsmToken::EndOfStatement))
Daniel Dunbara0d14262009-06-24 23:30:00 +0000863 return TokError("unexpected token in '.fill' directive");
864
865 Lexer.Lex();
866
867 if (FillSize != 1 && FillSize != 2 && FillSize != 4)
868 return TokError("invalid '.fill' size, expected 1, 2, or 4");
869
870 for (uint64_t i = 0, e = NumValues; i != e; ++i)
871 Out.EmitValue(MCValue::get(FillExpr), FillSize);
872
873 return false;
874}
Daniel Dunbarc238b582009-06-25 22:44:51 +0000875
876/// ParseDirectiveOrg
877/// ::= .org expression [ , expression ]
878bool AsmParser::ParseDirectiveOrg() {
Daniel Dunbarf4b830f2009-06-30 02:10:03 +0000879 MCValue Offset;
880 if (ParseRelocatableExpression(Offset))
Daniel Dunbarc238b582009-06-25 22:44:51 +0000881 return true;
882
883 // Parse optional fill expression.
884 int64_t FillExpr = 0;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000885 if (Lexer.isNot(AsmToken::EndOfStatement)) {
886 if (Lexer.isNot(AsmToken::Comma))
Daniel Dunbarc238b582009-06-25 22:44:51 +0000887 return TokError("unexpected token in '.org' directive");
888 Lexer.Lex();
889
Daniel Dunbar475839e2009-06-29 20:37:27 +0000890 if (ParseAbsoluteExpression(FillExpr))
Daniel Dunbarc238b582009-06-25 22:44:51 +0000891 return true;
892
Daniel Dunbar3f872332009-07-28 16:08:33 +0000893 if (Lexer.isNot(AsmToken::EndOfStatement))
Daniel Dunbarc238b582009-06-25 22:44:51 +0000894 return TokError("unexpected token in '.org' directive");
895 }
896
897 Lexer.Lex();
Daniel Dunbarf4b830f2009-06-30 02:10:03 +0000898
899 // FIXME: Only limited forms of relocatable expressions are accepted here, it
900 // has to be relative to the current section.
901 Out.EmitValueToOffset(Offset, FillExpr);
Daniel Dunbarc238b582009-06-25 22:44:51 +0000902
903 return false;
904}
Daniel Dunbarc29dfa72009-06-29 23:46:59 +0000905
906/// ParseDirectiveAlign
907/// ::= {.align, ...} expression [ , expression [ , expression ]]
908bool AsmParser::ParseDirectiveAlign(bool IsPow2, unsigned ValueSize) {
909 int64_t Alignment;
910 if (ParseAbsoluteExpression(Alignment))
911 return true;
912
913 SMLoc MaxBytesLoc;
914 bool HasFillExpr = false;
915 int64_t FillExpr = 0;
916 int64_t MaxBytesToFill = 0;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000917 if (Lexer.isNot(AsmToken::EndOfStatement)) {
918 if (Lexer.isNot(AsmToken::Comma))
Daniel Dunbarc29dfa72009-06-29 23:46:59 +0000919 return TokError("unexpected token in directive");
920 Lexer.Lex();
921
922 // The fill expression can be omitted while specifying a maximum number of
923 // alignment bytes, e.g:
924 // .align 3,,4
Daniel Dunbar3f872332009-07-28 16:08:33 +0000925 if (Lexer.isNot(AsmToken::Comma)) {
Daniel Dunbarc29dfa72009-06-29 23:46:59 +0000926 HasFillExpr = true;
927 if (ParseAbsoluteExpression(FillExpr))
928 return true;
929 }
930
Daniel Dunbar3f872332009-07-28 16:08:33 +0000931 if (Lexer.isNot(AsmToken::EndOfStatement)) {
932 if (Lexer.isNot(AsmToken::Comma))
Daniel Dunbarc29dfa72009-06-29 23:46:59 +0000933 return TokError("unexpected token in directive");
934 Lexer.Lex();
935
936 MaxBytesLoc = Lexer.getLoc();
937 if (ParseAbsoluteExpression(MaxBytesToFill))
938 return true;
939
Daniel Dunbar3f872332009-07-28 16:08:33 +0000940 if (Lexer.isNot(AsmToken::EndOfStatement))
Daniel Dunbarc29dfa72009-06-29 23:46:59 +0000941 return TokError("unexpected token in directive");
942 }
943 }
944
945 Lexer.Lex();
946
947 if (!HasFillExpr) {
948 // FIXME: Sometimes fill with nop.
949 FillExpr = 0;
950 }
951
952 // Compute alignment in bytes.
953 if (IsPow2) {
954 // FIXME: Diagnose overflow.
Chris Lattner39750252009-07-11 22:32:37 +0000955 Alignment = 1LL << Alignment;
Daniel Dunbarc29dfa72009-06-29 23:46:59 +0000956 }
957
958 // Diagnose non-sensical max bytes to fill.
959 if (MaxBytesLoc.isValid()) {
960 if (MaxBytesToFill < 1) {
Daniel Dunbar3fb76832009-06-30 00:49:23 +0000961 Warning(MaxBytesLoc, "alignment directive can never be satisfied in this "
962 "many bytes, ignoring");
Daniel Dunbarc29dfa72009-06-29 23:46:59 +0000963 return false;
964 }
965
966 if (MaxBytesToFill >= Alignment) {
Daniel Dunbar3fb76832009-06-30 00:49:23 +0000967 Warning(MaxBytesLoc, "maximum bytes expression exceeds alignment and "
968 "has no effect");
Daniel Dunbarc29dfa72009-06-29 23:46:59 +0000969 MaxBytesToFill = 0;
970 }
971 }
972
973 // FIXME: Target specific behavior about how the "extra" bytes are filled.
974 Out.EmitValueToAlignment(Alignment, FillExpr, ValueSize, MaxBytesToFill);
975
976 return false;
977}
978
Daniel Dunbard7b267b2009-06-30 00:33:19 +0000979/// ParseDirectiveSymbolAttribute
980/// ::= { ".globl", ".weak", ... } [ identifier ( , identifier )* ]
981bool AsmParser::ParseDirectiveSymbolAttribute(MCStreamer::SymbolAttr Attr) {
Daniel Dunbar3f872332009-07-28 16:08:33 +0000982 if (Lexer.isNot(AsmToken::EndOfStatement)) {
Daniel Dunbard7b267b2009-06-30 00:33:19 +0000983 for (;;) {
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +0000984 StringRef Name;
985
986 if (ParseIdentifier(Name))
Daniel Dunbard7b267b2009-06-30 00:33:19 +0000987 return TokError("expected identifier in directive");
988
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +0000989 MCSymbol *Sym = Ctx.GetOrCreateSymbol(Name);
Daniel Dunbard7b267b2009-06-30 00:33:19 +0000990
991 // If this is use of an undefined symbol then mark it external.
992 if (!Sym->getSection() && !Ctx.GetSymbolValue(Sym))
993 Sym->setExternal(true);
994
995 Out.EmitSymbolAttribute(Sym, Attr);
996
Daniel Dunbar3f872332009-07-28 16:08:33 +0000997 if (Lexer.is(AsmToken::EndOfStatement))
Daniel Dunbard7b267b2009-06-30 00:33:19 +0000998 break;
999
Daniel Dunbar3f872332009-07-28 16:08:33 +00001000 if (Lexer.isNot(AsmToken::Comma))
Daniel Dunbard7b267b2009-06-30 00:33:19 +00001001 return TokError("unexpected token in directive");
1002 Lexer.Lex();
1003 }
1004 }
1005
1006 Lexer.Lex();
1007 return false;
1008}
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001009
Kevin Enderby95cf30c2009-07-14 18:17:10 +00001010/// ParseDirectiveDarwinSymbolDesc
1011/// ::= .desc identifier , expression
1012bool AsmParser::ParseDirectiveDarwinSymbolDesc() {
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00001013 StringRef Name;
1014 if (ParseIdentifier(Name))
Kevin Enderby95cf30c2009-07-14 18:17:10 +00001015 return TokError("expected identifier in directive");
1016
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00001017 // Handle the identifier as the key symbol.
1018 MCSymbol *Sym = Ctx.GetOrCreateSymbol(Name);
Kevin Enderby95cf30c2009-07-14 18:17:10 +00001019
Daniel Dunbar3f872332009-07-28 16:08:33 +00001020 if (Lexer.isNot(AsmToken::Comma))
Kevin Enderby95cf30c2009-07-14 18:17:10 +00001021 return TokError("unexpected token in '.desc' directive");
1022 Lexer.Lex();
1023
1024 SMLoc DescLoc = Lexer.getLoc();
1025 int64_t DescValue;
1026 if (ParseAbsoluteExpression(DescValue))
1027 return true;
1028
Daniel Dunbar3f872332009-07-28 16:08:33 +00001029 if (Lexer.isNot(AsmToken::EndOfStatement))
Kevin Enderby95cf30c2009-07-14 18:17:10 +00001030 return TokError("unexpected token in '.desc' directive");
1031
1032 Lexer.Lex();
1033
1034 // Set the n_desc field of this Symbol to this DescValue
1035 Out.EmitSymbolDesc(Sym, DescValue);
1036
1037 return false;
1038}
1039
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001040/// ParseDirectiveComm
Chris Lattner1fc3d752009-07-09 17:25:12 +00001041/// ::= ( .comm | .lcomm ) identifier , size_expression [ , align_expression ]
1042bool AsmParser::ParseDirectiveComm(bool IsLocal) {
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00001043 SMLoc IDLoc = Lexer.getLoc();
1044 StringRef Name;
1045 if (ParseIdentifier(Name))
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001046 return TokError("expected identifier in directive");
1047
Daniel Dunbar76c4d762009-07-31 21:55:09 +00001048 // Handle the identifier as the key symbol.
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00001049 MCSymbol *Sym = Ctx.GetOrCreateSymbol(Name);
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001050
Daniel Dunbar3f872332009-07-28 16:08:33 +00001051 if (Lexer.isNot(AsmToken::Comma))
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001052 return TokError("unexpected token in directive");
1053 Lexer.Lex();
1054
1055 int64_t Size;
1056 SMLoc SizeLoc = Lexer.getLoc();
1057 if (ParseAbsoluteExpression(Size))
1058 return true;
1059
1060 int64_t Pow2Alignment = 0;
1061 SMLoc Pow2AlignmentLoc;
Daniel Dunbar3f872332009-07-28 16:08:33 +00001062 if (Lexer.is(AsmToken::Comma)) {
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001063 Lexer.Lex();
1064 Pow2AlignmentLoc = Lexer.getLoc();
1065 if (ParseAbsoluteExpression(Pow2Alignment))
1066 return true;
1067 }
1068
Daniel Dunbar3f872332009-07-28 16:08:33 +00001069 if (Lexer.isNot(AsmToken::EndOfStatement))
Chris Lattner1fc3d752009-07-09 17:25:12 +00001070 return TokError("unexpected token in '.comm' or '.lcomm' directive");
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001071
1072 Lexer.Lex();
1073
Chris Lattner1fc3d752009-07-09 17:25:12 +00001074 // NOTE: a size of zero for a .comm should create a undefined symbol
1075 // but a size of .lcomm creates a bss symbol of size zero.
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001076 if (Size < 0)
Chris Lattner1fc3d752009-07-09 17:25:12 +00001077 return Error(SizeLoc, "invalid '.comm' or '.lcomm' directive size, can't "
1078 "be less than zero");
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001079
1080 // NOTE: The alignment in the directive is a power of 2 value, the assember
1081 // may internally end up wanting an alignment in bytes.
1082 // FIXME: Diagnose overflow.
1083 if (Pow2Alignment < 0)
Chris Lattner1fc3d752009-07-09 17:25:12 +00001084 return Error(Pow2AlignmentLoc, "invalid '.comm' or '.lcomm' directive "
1085 "alignment, can't be less than zero");
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001086
1087 // TODO: Symbol must be undefined or it is a error to re-defined the symbol
1088 if (Sym->getSection() || Ctx.GetSymbolValue(Sym))
1089 return Error(IDLoc, "invalid symbol redefinition");
1090
Chris Lattner1fc3d752009-07-09 17:25:12 +00001091 // Create the Symbol as a common or local common with Size and Pow2Alignment
1092 Out.EmitCommonSymbol(Sym, Size, Pow2Alignment, IsLocal);
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001093
1094 return false;
1095}
Chris Lattner9be3fee2009-07-10 22:20:30 +00001096
1097/// ParseDirectiveDarwinZerofill
1098/// ::= .zerofill segname , sectname [, identifier , size_expression [
1099/// , align_expression ]]
1100bool AsmParser::ParseDirectiveDarwinZerofill() {
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00001101 // FIXME: Handle quoted names here.
1102
Daniel Dunbar3f872332009-07-28 16:08:33 +00001103 if (Lexer.isNot(AsmToken::Identifier))
Chris Lattner9be3fee2009-07-10 22:20:30 +00001104 return TokError("expected segment name after '.zerofill' directive");
Chris Lattnerff4bc462009-08-10 01:39:42 +00001105 StringRef Segment = Lexer.getTok().getString();
Chris Lattner9be3fee2009-07-10 22:20:30 +00001106 Lexer.Lex();
1107
Daniel Dunbar3f872332009-07-28 16:08:33 +00001108 if (Lexer.isNot(AsmToken::Comma))
Chris Lattner9be3fee2009-07-10 22:20:30 +00001109 return TokError("unexpected token in directive");
Chris Lattner9be3fee2009-07-10 22:20:30 +00001110 Lexer.Lex();
1111
Daniel Dunbar3f872332009-07-28 16:08:33 +00001112 if (Lexer.isNot(AsmToken::Identifier))
Chris Lattner9be3fee2009-07-10 22:20:30 +00001113 return TokError("expected section name after comma in '.zerofill' "
1114 "directive");
Chris Lattnerff4bc462009-08-10 01:39:42 +00001115 StringRef Section = Lexer.getTok().getString();
Chris Lattner9be3fee2009-07-10 22:20:30 +00001116 Lexer.Lex();
1117
Chris Lattner9be3fee2009-07-10 22:20:30 +00001118 // If this is the end of the line all that was wanted was to create the
1119 // the section but with no symbol.
Daniel Dunbar3f872332009-07-28 16:08:33 +00001120 if (Lexer.is(AsmToken::EndOfStatement)) {
Chris Lattnerff4bc462009-08-10 01:39:42 +00001121 // FIXME: CACHE THIS.
1122 MCSection *S = 0; //Ctx.GetSection(Section);
Chris Lattner6bdd74c2009-07-31 18:27:48 +00001123 if (S == 0)
Chris Lattnerff4bc462009-08-10 01:39:42 +00001124 S = MCSectionMachO::Create(Segment, Section,
1125 MCSectionMachO::S_ZEROFILL, 0,
1126 SectionKind(), Ctx);
Chris Lattner6bdd74c2009-07-31 18:27:48 +00001127
Chris Lattner9be3fee2009-07-10 22:20:30 +00001128 // Create the zerofill section but no symbol
Chris Lattner6bdd74c2009-07-31 18:27:48 +00001129 Out.EmitZerofill(S);
Chris Lattner9be3fee2009-07-10 22:20:30 +00001130 return false;
1131 }
1132
Daniel Dunbar3f872332009-07-28 16:08:33 +00001133 if (Lexer.isNot(AsmToken::Comma))
Chris Lattner9be3fee2009-07-10 22:20:30 +00001134 return TokError("unexpected token in directive");
1135 Lexer.Lex();
1136
Daniel Dunbar3f872332009-07-28 16:08:33 +00001137 if (Lexer.isNot(AsmToken::Identifier))
Chris Lattner9be3fee2009-07-10 22:20:30 +00001138 return TokError("expected identifier in directive");
1139
1140 // handle the identifier as the key symbol.
1141 SMLoc IDLoc = Lexer.getLoc();
Daniel Dunbar419aded2009-07-28 16:38:40 +00001142 MCSymbol *Sym = Ctx.GetOrCreateSymbol(Lexer.getTok().getString());
Chris Lattner9be3fee2009-07-10 22:20:30 +00001143 Lexer.Lex();
1144
Daniel Dunbar3f872332009-07-28 16:08:33 +00001145 if (Lexer.isNot(AsmToken::Comma))
Chris Lattner9be3fee2009-07-10 22:20:30 +00001146 return TokError("unexpected token in directive");
1147 Lexer.Lex();
1148
1149 int64_t Size;
1150 SMLoc SizeLoc = Lexer.getLoc();
1151 if (ParseAbsoluteExpression(Size))
1152 return true;
1153
1154 int64_t Pow2Alignment = 0;
1155 SMLoc Pow2AlignmentLoc;
Daniel Dunbar3f872332009-07-28 16:08:33 +00001156 if (Lexer.is(AsmToken::Comma)) {
Chris Lattner9be3fee2009-07-10 22:20:30 +00001157 Lexer.Lex();
1158 Pow2AlignmentLoc = Lexer.getLoc();
1159 if (ParseAbsoluteExpression(Pow2Alignment))
1160 return true;
1161 }
1162
Daniel Dunbar3f872332009-07-28 16:08:33 +00001163 if (Lexer.isNot(AsmToken::EndOfStatement))
Chris Lattner9be3fee2009-07-10 22:20:30 +00001164 return TokError("unexpected token in '.zerofill' directive");
1165
1166 Lexer.Lex();
1167
1168 if (Size < 0)
1169 return Error(SizeLoc, "invalid '.zerofill' directive size, can't be less "
1170 "than zero");
1171
1172 // NOTE: The alignment in the directive is a power of 2 value, the assember
1173 // may internally end up wanting an alignment in bytes.
1174 // FIXME: Diagnose overflow.
1175 if (Pow2Alignment < 0)
1176 return Error(Pow2AlignmentLoc, "invalid '.zerofill' directive alignment, "
1177 "can't be less than zero");
1178
1179 // TODO: Symbol must be undefined or it is a error to re-defined the symbol
1180 if (Sym->getSection() || Ctx.GetSymbolValue(Sym))
1181 return Error(IDLoc, "invalid symbol redefinition");
1182
Chris Lattner6bdd74c2009-07-31 18:27:48 +00001183 // FIXME: Arch specific.
Chris Lattnerff4bc462009-08-10 01:39:42 +00001184 // FIXME: CACHE.
1185 MCSection *S = 0; //Ctx.GetSection(Section);
Chris Lattner6bdd74c2009-07-31 18:27:48 +00001186 if (S == 0)
Chris Lattnerff4bc462009-08-10 01:39:42 +00001187 S = MCSectionMachO::Create(Segment, Section,
1188 MCSectionMachO::S_ZEROFILL, 0,
1189 SectionKind(), Ctx);
Chris Lattner6bdd74c2009-07-31 18:27:48 +00001190
Chris Lattner9be3fee2009-07-10 22:20:30 +00001191 // Create the zerofill Symbol with Size and Pow2Alignment
Chris Lattner6bdd74c2009-07-31 18:27:48 +00001192 Out.EmitZerofill(S, Sym, Size, Pow2Alignment);
Chris Lattner9be3fee2009-07-10 22:20:30 +00001193
1194 return false;
1195}
Kevin Enderbya5c78322009-07-13 21:03:15 +00001196
1197/// ParseDirectiveDarwinSubsectionsViaSymbols
1198/// ::= .subsections_via_symbols
1199bool AsmParser::ParseDirectiveDarwinSubsectionsViaSymbols() {
Daniel Dunbar3f872332009-07-28 16:08:33 +00001200 if (Lexer.isNot(AsmToken::EndOfStatement))
Kevin Enderbya5c78322009-07-13 21:03:15 +00001201 return TokError("unexpected token in '.subsections_via_symbols' directive");
1202
1203 Lexer.Lex();
1204
Kevin Enderbyf96db462009-07-16 17:56:39 +00001205 Out.EmitAssemblerFlag(MCStreamer::SubsectionsViaSymbols);
Kevin Enderbya5c78322009-07-13 21:03:15 +00001206
1207 return false;
1208}
Kevin Enderby5f1f0b82009-07-13 23:15:14 +00001209
1210/// ParseDirectiveAbort
1211/// ::= .abort [ "abort_string" ]
1212bool AsmParser::ParseDirectiveAbort() {
Daniel Dunbarf9507ff2009-07-27 23:20:52 +00001213 // FIXME: Use loc from directive.
1214 SMLoc Loc = Lexer.getLoc();
1215
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001216 StringRef Str = "";
Daniel Dunbar3f872332009-07-28 16:08:33 +00001217 if (Lexer.isNot(AsmToken::EndOfStatement)) {
1218 if (Lexer.isNot(AsmToken::String))
Kevin Enderby5f1f0b82009-07-13 23:15:14 +00001219 return TokError("expected string in '.abort' directive");
1220
Daniel Dunbar419aded2009-07-28 16:38:40 +00001221 Str = Lexer.getTok().getString();
Kevin Enderby5f1f0b82009-07-13 23:15:14 +00001222
1223 Lexer.Lex();
1224 }
1225
Daniel Dunbar3f872332009-07-28 16:08:33 +00001226 if (Lexer.isNot(AsmToken::EndOfStatement))
Kevin Enderby5f1f0b82009-07-13 23:15:14 +00001227 return TokError("unexpected token in '.abort' directive");
1228
1229 Lexer.Lex();
1230
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001231 // FIXME: Handle here.
Daniel Dunbarf9507ff2009-07-27 23:20:52 +00001232 if (Str.empty())
1233 Error(Loc, ".abort detected. Assembly stopping.");
1234 else
1235 Error(Loc, ".abort '" + Str + "' detected. Assembly stopping.");
Kevin Enderby5f1f0b82009-07-13 23:15:14 +00001236
1237 return false;
1238}
Kevin Enderby71148242009-07-14 21:35:03 +00001239
1240/// ParseDirectiveLsym
1241/// ::= .lsym identifier , expression
1242bool AsmParser::ParseDirectiveDarwinLsym() {
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00001243 StringRef Name;
1244 if (ParseIdentifier(Name))
Kevin Enderby71148242009-07-14 21:35:03 +00001245 return TokError("expected identifier in directive");
1246
Daniel Dunbar76c4d762009-07-31 21:55:09 +00001247 // Handle the identifier as the key symbol.
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00001248 MCSymbol *Sym = Ctx.GetOrCreateSymbol(Name);
Kevin Enderby71148242009-07-14 21:35:03 +00001249
Daniel Dunbar3f872332009-07-28 16:08:33 +00001250 if (Lexer.isNot(AsmToken::Comma))
Kevin Enderby71148242009-07-14 21:35:03 +00001251 return TokError("unexpected token in '.lsym' directive");
1252 Lexer.Lex();
1253
1254 MCValue Expr;
1255 if (ParseRelocatableExpression(Expr))
1256 return true;
1257
Daniel Dunbar3f872332009-07-28 16:08:33 +00001258 if (Lexer.isNot(AsmToken::EndOfStatement))
Kevin Enderby71148242009-07-14 21:35:03 +00001259 return TokError("unexpected token in '.lsym' directive");
1260
1261 Lexer.Lex();
1262
1263 // Create the Sym with the value of the Expr
1264 Out.EmitLocalSymbol(Sym, Expr);
1265
1266 return false;
1267}
Kevin Enderby1f049b22009-07-14 23:21:55 +00001268
1269/// ParseDirectiveInclude
1270/// ::= .include "filename"
1271bool AsmParser::ParseDirectiveInclude() {
Daniel Dunbar3f872332009-07-28 16:08:33 +00001272 if (Lexer.isNot(AsmToken::String))
Kevin Enderby1f049b22009-07-14 23:21:55 +00001273 return TokError("expected string in '.include' directive");
1274
Daniel Dunbar419aded2009-07-28 16:38:40 +00001275 std::string Filename = Lexer.getTok().getString();
Chris Lattner8e25e2d2009-07-16 06:14:39 +00001276 SMLoc IncludeLoc = Lexer.getLoc();
Kevin Enderby1f049b22009-07-14 23:21:55 +00001277 Lexer.Lex();
1278
Daniel Dunbar3f872332009-07-28 16:08:33 +00001279 if (Lexer.isNot(AsmToken::EndOfStatement))
Kevin Enderby1f049b22009-07-14 23:21:55 +00001280 return TokError("unexpected token in '.include' directive");
1281
Chris Lattner8e25e2d2009-07-16 06:14:39 +00001282 // Strip the quotes.
1283 Filename = Filename.substr(1, Filename.size()-2);
1284
1285 // Attempt to switch the lexer to the included file before consuming the end
1286 // of statement to avoid losing it when we switch.
1287 if (Lexer.EnterIncludeFile(Filename)) {
1288 Lexer.PrintMessage(IncludeLoc,
1289 "Could not find include file '" + Filename + "'",
1290 "error");
1291 return true;
1292 }
Kevin Enderby1f049b22009-07-14 23:21:55 +00001293
1294 return false;
1295}
Kevin Enderby6e68cd92009-07-15 15:30:11 +00001296
1297/// ParseDirectiveDarwinDumpOrLoad
1298/// ::= ( .dump | .load ) "filename"
Kevin Enderby5026ae42009-07-20 20:25:37 +00001299bool AsmParser::ParseDirectiveDarwinDumpOrLoad(SMLoc IDLoc, bool IsDump) {
Daniel Dunbar3f872332009-07-28 16:08:33 +00001300 if (Lexer.isNot(AsmToken::String))
Kevin Enderby6e68cd92009-07-15 15:30:11 +00001301 return TokError("expected string in '.dump' or '.load' directive");
1302
Kevin Enderby6e68cd92009-07-15 15:30:11 +00001303 Lexer.Lex();
1304
Daniel Dunbar3f872332009-07-28 16:08:33 +00001305 if (Lexer.isNot(AsmToken::EndOfStatement))
Kevin Enderby6e68cd92009-07-15 15:30:11 +00001306 return TokError("unexpected token in '.dump' or '.load' directive");
1307
1308 Lexer.Lex();
1309
Kevin Enderby5026ae42009-07-20 20:25:37 +00001310 // FIXME: If/when .dump and .load are implemented they will be done in the
1311 // the assembly parser and not have any need for an MCStreamer API.
Kevin Enderby6e68cd92009-07-15 15:30:11 +00001312 if (IsDump)
Kevin Enderby5026ae42009-07-20 20:25:37 +00001313 Warning(IDLoc, "ignoring directive .dump for now");
Kevin Enderby6e68cd92009-07-15 15:30:11 +00001314 else
Kevin Enderby5026ae42009-07-20 20:25:37 +00001315 Warning(IDLoc, "ignoring directive .load for now");
Kevin Enderby6e68cd92009-07-15 15:30:11 +00001316
1317 return false;
1318}
Kevin Enderbyc114ed72009-08-07 22:46:00 +00001319
1320/// ParseDirectiveIf
1321/// ::= .if expression
1322bool AsmParser::ParseDirectiveIf(SMLoc DirectiveLoc) {
1323 // Consume the identifier that was the .if directive
1324 Lexer.Lex();
1325
1326 TheCondStack.push_back(TheCondState);
1327 TheCondState.TheCond = AsmCond::IfCond;
1328 if(TheCondState.Ignore) {
1329 EatToEndOfStatement();
1330 }
1331 else {
1332 int64_t ExprValue;
1333 if (ParseAbsoluteExpression(ExprValue))
1334 return true;
1335
1336 if (Lexer.isNot(AsmToken::EndOfStatement))
1337 return TokError("unexpected token in '.if' directive");
1338
1339 Lexer.Lex();
1340
1341 TheCondState.CondMet = ExprValue;
1342 TheCondState.Ignore = !TheCondState.CondMet;
1343 }
1344
1345 return false;
1346}
1347
1348/// ParseDirectiveElseIf
1349/// ::= .elseif expression
1350bool AsmParser::ParseDirectiveElseIf(SMLoc DirectiveLoc) {
1351 if (TheCondState.TheCond != AsmCond::IfCond &&
1352 TheCondState.TheCond != AsmCond::ElseIfCond)
1353 Error(DirectiveLoc, "Encountered a .elseif that doesn't follow a .if or "
1354 " an .elseif");
1355 TheCondState.TheCond = AsmCond::ElseIfCond;
1356
1357 // Consume the identifier that was the .elseif directive
1358 Lexer.Lex();
1359
1360 bool LastIgnoreState = false;
1361 if (!TheCondStack.empty())
1362 LastIgnoreState = TheCondStack.back().Ignore;
1363 if (LastIgnoreState || TheCondState.CondMet) {
1364 TheCondState.Ignore = true;
1365 EatToEndOfStatement();
1366 }
1367 else {
1368 int64_t ExprValue;
1369 if (ParseAbsoluteExpression(ExprValue))
1370 return true;
1371
1372 if (Lexer.isNot(AsmToken::EndOfStatement))
1373 return TokError("unexpected token in '.elseif' directive");
1374
1375 Lexer.Lex();
1376 TheCondState.CondMet = ExprValue;
1377 TheCondState.Ignore = !TheCondState.CondMet;
1378 }
1379
1380 return false;
1381}
1382
1383/// ParseDirectiveElse
1384/// ::= .else
1385bool AsmParser::ParseDirectiveElse(SMLoc DirectiveLoc) {
1386 // Consume the identifier that was the .else directive
1387 Lexer.Lex();
1388
1389 if (Lexer.isNot(AsmToken::EndOfStatement))
1390 return TokError("unexpected token in '.else' directive");
1391
1392 Lexer.Lex();
1393
1394 if (TheCondState.TheCond != AsmCond::IfCond &&
1395 TheCondState.TheCond != AsmCond::ElseIfCond)
1396 Error(DirectiveLoc, "Encountered a .else that doesn't follow a .if or an "
1397 ".elseif");
1398 TheCondState.TheCond = AsmCond::ElseCond;
1399 bool LastIgnoreState = false;
1400 if (!TheCondStack.empty())
1401 LastIgnoreState = TheCondStack.back().Ignore;
1402 if (LastIgnoreState || TheCondState.CondMet)
1403 TheCondState.Ignore = true;
1404 else
1405 TheCondState.Ignore = false;
1406
1407 return false;
1408}
1409
1410/// ParseDirectiveEndIf
1411/// ::= .endif
1412bool AsmParser::ParseDirectiveEndIf(SMLoc DirectiveLoc) {
1413 // Consume the identifier that was the .endif directive
1414 Lexer.Lex();
1415
1416 if (Lexer.isNot(AsmToken::EndOfStatement))
1417 return TokError("unexpected token in '.endif' directive");
1418
1419 Lexer.Lex();
1420
1421 if ((TheCondState.TheCond == AsmCond::NoCond) ||
1422 TheCondStack.empty())
1423 Error(DirectiveLoc, "Encountered a .endif that doesn't follow a .if or "
1424 ".else");
1425 if (!TheCondStack.empty()) {
1426 TheCondState = TheCondStack.back();
1427 TheCondStack.pop_back();
1428 }
1429
1430 return false;
1431}