blob: 29657de5d86c5ff6d02d709875eede9a9d50aba0 [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.
407 return ParseDirectiveSectionSwitch("__TEXT,__text",
408 "regular,pure_instructions");
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000409 if (IDVal == ".const")
Chris Lattner529fb542009-06-24 05:13:15 +0000410 return ParseDirectiveSectionSwitch("__TEXT,__const");
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000411 if (IDVal == ".static_const")
Chris Lattner529fb542009-06-24 05:13:15 +0000412 return ParseDirectiveSectionSwitch("__TEXT,__static_const");
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000413 if (IDVal == ".cstring")
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000414 return ParseDirectiveSectionSwitch("__TEXT,__cstring",
415 "cstring_literals");
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000416 if (IDVal == ".literal4")
Chris Lattner529fb542009-06-24 05:13:15 +0000417 return ParseDirectiveSectionSwitch("__TEXT,__literal4", "4byte_literals");
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000418 if (IDVal == ".literal8")
Chris Lattner529fb542009-06-24 05:13:15 +0000419 return ParseDirectiveSectionSwitch("__TEXT,__literal8", "8byte_literals");
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000420 if (IDVal == ".literal16")
Chris Lattner529fb542009-06-24 05:13:15 +0000421 return ParseDirectiveSectionSwitch("__TEXT,__literal16",
422 "16byte_literals");
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000423 if (IDVal == ".constructor")
Chris Lattner529fb542009-06-24 05:13:15 +0000424 return ParseDirectiveSectionSwitch("__TEXT,__constructor");
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000425 if (IDVal == ".destructor")
Chris Lattner529fb542009-06-24 05:13:15 +0000426 return ParseDirectiveSectionSwitch("__TEXT,__destructor");
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000427 if (IDVal == ".fvmlib_init0")
Chris Lattner529fb542009-06-24 05:13:15 +0000428 return ParseDirectiveSectionSwitch("__TEXT,__fvmlib_init0");
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000429 if (IDVal == ".fvmlib_init1")
Chris Lattner529fb542009-06-24 05:13:15 +0000430 return ParseDirectiveSectionSwitch("__TEXT,__fvmlib_init1");
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000431 if (IDVal == ".symbol_stub") // FIXME: Different on PPC.
Chris Lattner529fb542009-06-24 05:13:15 +0000432 return ParseDirectiveSectionSwitch("__IMPORT,__jump_table,symbol_stubs",
433 "self_modifying_code+pure_instructions,5");
434 // FIXME: .picsymbol_stub on PPC.
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000435 if (IDVal == ".data")
Chris Lattner529fb542009-06-24 05:13:15 +0000436 return ParseDirectiveSectionSwitch("__DATA,__data");
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000437 if (IDVal == ".static_data")
Chris Lattner529fb542009-06-24 05:13:15 +0000438 return ParseDirectiveSectionSwitch("__DATA,__static_data");
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000439 if (IDVal == ".non_lazy_symbol_pointer")
Chris Lattner529fb542009-06-24 05:13:15 +0000440 return ParseDirectiveSectionSwitch("__DATA,__nl_symbol_pointer",
441 "non_lazy_symbol_pointers");
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000442 if (IDVal == ".lazy_symbol_pointer")
Chris Lattner529fb542009-06-24 05:13:15 +0000443 return ParseDirectiveSectionSwitch("__DATA,__la_symbol_pointer",
444 "lazy_symbol_pointers");
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000445 if (IDVal == ".dyld")
Chris Lattner529fb542009-06-24 05:13:15 +0000446 return ParseDirectiveSectionSwitch("__DATA,__dyld");
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000447 if (IDVal == ".mod_init_func")
Chris Lattner529fb542009-06-24 05:13:15 +0000448 return ParseDirectiveSectionSwitch("__DATA,__mod_init_func",
449 "mod_init_funcs");
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000450 if (IDVal == ".mod_term_func")
Chris Lattner529fb542009-06-24 05:13:15 +0000451 return ParseDirectiveSectionSwitch("__DATA,__mod_term_func",
452 "mod_term_funcs");
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000453 if (IDVal == ".const_data")
Chris Lattner529fb542009-06-24 05:13:15 +0000454 return ParseDirectiveSectionSwitch("__DATA,__const", "regular");
455
456
457 // FIXME: Verify attributes on sections.
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000458 if (IDVal == ".objc_class")
Chris Lattner529fb542009-06-24 05:13:15 +0000459 return ParseDirectiveSectionSwitch("__OBJC,__class");
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000460 if (IDVal == ".objc_meta_class")
Chris Lattner529fb542009-06-24 05:13:15 +0000461 return ParseDirectiveSectionSwitch("__OBJC,__meta_class");
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000462 if (IDVal == ".objc_cat_cls_meth")
Chris Lattner529fb542009-06-24 05:13:15 +0000463 return ParseDirectiveSectionSwitch("__OBJC,__cat_cls_meth");
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000464 if (IDVal == ".objc_cat_inst_meth")
Chris Lattner529fb542009-06-24 05:13:15 +0000465 return ParseDirectiveSectionSwitch("__OBJC,__cat_inst_meth");
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000466 if (IDVal == ".objc_protocol")
Chris Lattner529fb542009-06-24 05:13:15 +0000467 return ParseDirectiveSectionSwitch("__OBJC,__protocol");
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000468 if (IDVal == ".objc_string_object")
Chris Lattner529fb542009-06-24 05:13:15 +0000469 return ParseDirectiveSectionSwitch("__OBJC,__string_object");
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000470 if (IDVal == ".objc_cls_meth")
Chris Lattner529fb542009-06-24 05:13:15 +0000471 return ParseDirectiveSectionSwitch("__OBJC,__cls_meth");
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000472 if (IDVal == ".objc_inst_meth")
Chris Lattner529fb542009-06-24 05:13:15 +0000473 return ParseDirectiveSectionSwitch("__OBJC,__inst_meth");
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000474 if (IDVal == ".objc_cls_refs")
Chris Lattner529fb542009-06-24 05:13:15 +0000475 return ParseDirectiveSectionSwitch("__OBJC,__cls_refs");
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000476 if (IDVal == ".objc_message_refs")
Chris Lattner529fb542009-06-24 05:13:15 +0000477 return ParseDirectiveSectionSwitch("__OBJC,__message_refs");
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000478 if (IDVal == ".objc_symbols")
Chris Lattner529fb542009-06-24 05:13:15 +0000479 return ParseDirectiveSectionSwitch("__OBJC,__symbols");
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000480 if (IDVal == ".objc_category")
Chris Lattner529fb542009-06-24 05:13:15 +0000481 return ParseDirectiveSectionSwitch("__OBJC,__category");
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000482 if (IDVal == ".objc_class_vars")
Chris Lattner529fb542009-06-24 05:13:15 +0000483 return ParseDirectiveSectionSwitch("__OBJC,__class_vars");
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000484 if (IDVal == ".objc_instance_vars")
Chris Lattner529fb542009-06-24 05:13:15 +0000485 return ParseDirectiveSectionSwitch("__OBJC,__instance_vars");
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000486 if (IDVal == ".objc_module_info")
Chris Lattner529fb542009-06-24 05:13:15 +0000487 return ParseDirectiveSectionSwitch("__OBJC,__module_info");
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000488 if (IDVal == ".objc_class_names")
Chris Lattner529fb542009-06-24 05:13:15 +0000489 return ParseDirectiveSectionSwitch("__TEXT,__cstring","cstring_literals");
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000490 if (IDVal == ".objc_meth_var_types")
Chris Lattner529fb542009-06-24 05:13:15 +0000491 return ParseDirectiveSectionSwitch("__TEXT,__cstring","cstring_literals");
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000492 if (IDVal == ".objc_meth_var_names")
Chris Lattner529fb542009-06-24 05:13:15 +0000493 return ParseDirectiveSectionSwitch("__TEXT,__cstring","cstring_literals");
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000494 if (IDVal == ".objc_selector_strs")
Chris Lattner529fb542009-06-24 05:13:15 +0000495 return ParseDirectiveSectionSwitch("__OBJC,__selector_strs");
Chris Lattner9a023f72009-06-24 04:43:34 +0000496
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000497 // Assembler features
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000498 if (IDVal == ".set")
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000499 return ParseDirectiveSet();
500
Daniel Dunbara0d14262009-06-24 23:30:00 +0000501 // Data directives
502
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000503 if (IDVal == ".ascii")
Daniel Dunbara0d14262009-06-24 23:30:00 +0000504 return ParseDirectiveAscii(false);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000505 if (IDVal == ".asciz")
Daniel Dunbara0d14262009-06-24 23:30:00 +0000506 return ParseDirectiveAscii(true);
507
508 // FIXME: Target hooks for size? Also for "word", "hword".
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000509 if (IDVal == ".byte")
Daniel Dunbara0d14262009-06-24 23:30:00 +0000510 return ParseDirectiveValue(1);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000511 if (IDVal == ".short")
Daniel Dunbara0d14262009-06-24 23:30:00 +0000512 return ParseDirectiveValue(2);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000513 if (IDVal == ".long")
Daniel Dunbara0d14262009-06-24 23:30:00 +0000514 return ParseDirectiveValue(4);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000515 if (IDVal == ".quad")
Daniel Dunbara0d14262009-06-24 23:30:00 +0000516 return ParseDirectiveValue(8);
Daniel Dunbarc29dfa72009-06-29 23:46:59 +0000517
518 // FIXME: Target hooks for IsPow2.
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000519 if (IDVal == ".align")
Daniel Dunbarc29dfa72009-06-29 23:46:59 +0000520 return ParseDirectiveAlign(/*IsPow2=*/true, /*ExprSize=*/1);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000521 if (IDVal == ".align32")
Daniel Dunbarc29dfa72009-06-29 23:46:59 +0000522 return ParseDirectiveAlign(/*IsPow2=*/true, /*ExprSize=*/4);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000523 if (IDVal == ".balign")
Daniel Dunbarc29dfa72009-06-29 23:46:59 +0000524 return ParseDirectiveAlign(/*IsPow2=*/false, /*ExprSize=*/1);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000525 if (IDVal == ".balignw")
Daniel Dunbarc29dfa72009-06-29 23:46:59 +0000526 return ParseDirectiveAlign(/*IsPow2=*/false, /*ExprSize=*/2);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000527 if (IDVal == ".balignl")
Daniel Dunbarc29dfa72009-06-29 23:46:59 +0000528 return ParseDirectiveAlign(/*IsPow2=*/false, /*ExprSize=*/4);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000529 if (IDVal == ".p2align")
Daniel Dunbarc29dfa72009-06-29 23:46:59 +0000530 return ParseDirectiveAlign(/*IsPow2=*/true, /*ExprSize=*/1);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000531 if (IDVal == ".p2alignw")
Daniel Dunbarc29dfa72009-06-29 23:46:59 +0000532 return ParseDirectiveAlign(/*IsPow2=*/true, /*ExprSize=*/2);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000533 if (IDVal == ".p2alignl")
Daniel Dunbarc29dfa72009-06-29 23:46:59 +0000534 return ParseDirectiveAlign(/*IsPow2=*/true, /*ExprSize=*/4);
535
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000536 if (IDVal == ".org")
Daniel Dunbarc238b582009-06-25 22:44:51 +0000537 return ParseDirectiveOrg();
Daniel Dunbarc29dfa72009-06-29 23:46:59 +0000538
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000539 if (IDVal == ".fill")
Daniel Dunbarc29dfa72009-06-29 23:46:59 +0000540 return ParseDirectiveFill();
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000541 if (IDVal == ".space")
Daniel Dunbara0d14262009-06-24 23:30:00 +0000542 return ParseDirectiveSpace();
543
Daniel Dunbard7b267b2009-06-30 00:33:19 +0000544 // Symbol attribute directives
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000545 if (IDVal == ".globl" || IDVal == ".global")
Daniel Dunbard7b267b2009-06-30 00:33:19 +0000546 return ParseDirectiveSymbolAttribute(MCStreamer::Global);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000547 if (IDVal == ".hidden")
Daniel Dunbard7b267b2009-06-30 00:33:19 +0000548 return ParseDirectiveSymbolAttribute(MCStreamer::Hidden);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000549 if (IDVal == ".indirect_symbol")
Daniel Dunbard7b267b2009-06-30 00:33:19 +0000550 return ParseDirectiveSymbolAttribute(MCStreamer::IndirectSymbol);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000551 if (IDVal == ".internal")
Daniel Dunbard7b267b2009-06-30 00:33:19 +0000552 return ParseDirectiveSymbolAttribute(MCStreamer::Internal);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000553 if (IDVal == ".lazy_reference")
Daniel Dunbard7b267b2009-06-30 00:33:19 +0000554 return ParseDirectiveSymbolAttribute(MCStreamer::LazyReference);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000555 if (IDVal == ".no_dead_strip")
Daniel Dunbard7b267b2009-06-30 00:33:19 +0000556 return ParseDirectiveSymbolAttribute(MCStreamer::NoDeadStrip);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000557 if (IDVal == ".private_extern")
Daniel Dunbard7b267b2009-06-30 00:33:19 +0000558 return ParseDirectiveSymbolAttribute(MCStreamer::PrivateExtern);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000559 if (IDVal == ".protected")
Daniel Dunbard7b267b2009-06-30 00:33:19 +0000560 return ParseDirectiveSymbolAttribute(MCStreamer::Protected);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000561 if (IDVal == ".reference")
Daniel Dunbard7b267b2009-06-30 00:33:19 +0000562 return ParseDirectiveSymbolAttribute(MCStreamer::Reference);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000563 if (IDVal == ".weak")
Daniel Dunbard7b267b2009-06-30 00:33:19 +0000564 return ParseDirectiveSymbolAttribute(MCStreamer::Weak);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000565 if (IDVal == ".weak_definition")
Daniel Dunbard7b267b2009-06-30 00:33:19 +0000566 return ParseDirectiveSymbolAttribute(MCStreamer::WeakDefinition);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000567 if (IDVal == ".weak_reference")
Daniel Dunbard7b267b2009-06-30 00:33:19 +0000568 return ParseDirectiveSymbolAttribute(MCStreamer::WeakReference);
569
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000570 if (IDVal == ".comm")
Chris Lattner1fc3d752009-07-09 17:25:12 +0000571 return ParseDirectiveComm(/*IsLocal=*/false);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000572 if (IDVal == ".lcomm")
Chris Lattner1fc3d752009-07-09 17:25:12 +0000573 return ParseDirectiveComm(/*IsLocal=*/true);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000574 if (IDVal == ".zerofill")
Chris Lattner9be3fee2009-07-10 22:20:30 +0000575 return ParseDirectiveDarwinZerofill();
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000576 if (IDVal == ".desc")
Kevin Enderby95cf30c2009-07-14 18:17:10 +0000577 return ParseDirectiveDarwinSymbolDesc();
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000578 if (IDVal == ".lsym")
Kevin Enderby71148242009-07-14 21:35:03 +0000579 return ParseDirectiveDarwinLsym();
Chris Lattner4e4db7a2009-07-07 20:30:46 +0000580
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000581 if (IDVal == ".subsections_via_symbols")
Kevin Enderbya5c78322009-07-13 21:03:15 +0000582 return ParseDirectiveDarwinSubsectionsViaSymbols();
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000583 if (IDVal == ".abort")
Kevin Enderby5f1f0b82009-07-13 23:15:14 +0000584 return ParseDirectiveAbort();
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000585 if (IDVal == ".include")
Kevin Enderby1f049b22009-07-14 23:21:55 +0000586 return ParseDirectiveInclude();
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000587 if (IDVal == ".dump")
Kevin Enderby5026ae42009-07-20 20:25:37 +0000588 return ParseDirectiveDarwinDumpOrLoad(IDLoc, /*IsDump=*/true);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000589 if (IDVal == ".load")
Kevin Enderby5026ae42009-07-20 20:25:37 +0000590 return ParseDirectiveDarwinDumpOrLoad(IDLoc, /*IsLoad=*/false);
Kevin Enderbya5c78322009-07-13 21:03:15 +0000591
Daniel Dunbar3fb76832009-06-30 00:49:23 +0000592 Warning(IDLoc, "ignoring directive for now");
Chris Lattner2cf5f142009-06-22 01:29:09 +0000593 EatToEndOfStatement();
594 return false;
595 }
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000596
Chris Lattner29dfe7c2009-06-23 18:41:30 +0000597 MCInst Inst;
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000598 if (getTargetParser().ParseInstruction(IDVal, Inst))
Chris Lattner29dfe7c2009-06-23 18:41:30 +0000599 return true;
Chris Lattner2cf5f142009-06-22 01:29:09 +0000600
Daniel Dunbar3f872332009-07-28 16:08:33 +0000601 if (Lexer.isNot(AsmToken::EndOfStatement))
Chris Lattner9a023f72009-06-24 04:43:34 +0000602 return TokError("unexpected token in argument list");
Chris Lattner2cf5f142009-06-22 01:29:09 +0000603
604 // Eat the end of statement marker.
605 Lexer.Lex();
606
607 // Instruction is good, process it.
Daniel Dunbar0eebb052009-07-01 06:35:48 +0000608 Out.EmitInstruction(Inst);
Chris Lattner2cf5f142009-06-22 01:29:09 +0000609
610 // Skip to end of line for now.
Chris Lattner27aa7d22009-06-21 20:16:42 +0000611 return false;
612}
Chris Lattner9a023f72009-06-24 04:43:34 +0000613
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000614bool AsmParser::ParseAssignment(const StringRef &Name, bool IsDotSet) {
Daniel Dunbardce0f3c2009-06-29 23:43:14 +0000615 // FIXME: Use better location, we should use proper tokens.
616 SMLoc EqualLoc = Lexer.getLoc();
617
Daniel Dunbarf4b830f2009-06-30 02:10:03 +0000618 MCValue Value;
619 if (ParseRelocatableExpression(Value))
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000620 return true;
621
Daniel Dunbar3f872332009-07-28 16:08:33 +0000622 if (Lexer.isNot(AsmToken::EndOfStatement))
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000623 return TokError("unexpected token in assignment");
624
625 // Eat the end of statement marker.
626 Lexer.Lex();
627
Daniel Dunbardce0f3c2009-06-29 23:43:14 +0000628 // Diagnose assignment to a label.
629 //
630 // FIXME: Diagnostics. Note the location of the definition as a label.
631 // FIXME: This doesn't diagnose assignment to a symbol which has been
632 // implicitly marked as external.
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000633 // FIXME: Handle '.'.
Daniel Dunbardce0f3c2009-06-29 23:43:14 +0000634 // FIXME: Diagnose assignment to protected identifier (e.g., register name).
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000635 MCSymbol *Sym = Ctx.GetOrCreateSymbol(Name);
Daniel Dunbardce0f3c2009-06-29 23:43:14 +0000636 if (Sym->getSection())
637 return Error(EqualLoc, "invalid assignment to symbol emitted as a label");
638 if (Sym->isExternal())
639 return Error(EqualLoc, "invalid assignment to external symbol");
640
641 // Do the assignment.
Daniel Dunbarf4b830f2009-06-30 02:10:03 +0000642 Out.EmitAssignment(Sym, Value, IsDotSet);
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000643
644 return false;
645}
646
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +0000647/// ParseIdentifier:
648/// ::= identifier
649/// ::= string
650bool AsmParser::ParseIdentifier(StringRef &Res) {
651 if (Lexer.isNot(AsmToken::Identifier) &&
652 Lexer.isNot(AsmToken::String))
653 return true;
654
655 Res = Lexer.getTok().getIdentifier();
656
657 Lexer.Lex(); // Consume the identifier token.
658
659 return false;
660}
661
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000662/// ParseDirectiveSet:
663/// ::= .set identifier ',' expression
664bool AsmParser::ParseDirectiveSet() {
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +0000665 StringRef Name;
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000666
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +0000667 if (ParseIdentifier(Name))
668 return TokError("expected identifier after '.set' directive");
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000669
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +0000670 if (Lexer.isNot(AsmToken::Comma))
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000671 return TokError("unexpected token in '.set'");
672 Lexer.Lex();
673
674 return ParseAssignment(Name, true);
675}
676
Chris Lattner9a023f72009-06-24 04:43:34 +0000677/// ParseDirectiveSection:
Chris Lattner529fb542009-06-24 05:13:15 +0000678/// ::= .section identifier (',' identifier)*
679/// FIXME: This should actually parse out the segment, section, attributes and
680/// sizeof_stub fields.
681bool AsmParser::ParseDirectiveDarwinSection() {
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +0000682 StringRef SectionName;
683
684 if (ParseIdentifier(SectionName))
Chris Lattner9a023f72009-06-24 04:43:34 +0000685 return TokError("expected identifier after '.section' directive");
686
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +0000687 std::string Section = SectionName;
688
689 // FIXME: This doesn't work, we lose quoting on things
690
Chris Lattner9a023f72009-06-24 04:43:34 +0000691 // Accept a comma separated list of modifiers.
Daniel Dunbar3f872332009-07-28 16:08:33 +0000692 while (Lexer.is(AsmToken::Comma)) {
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +0000693 Lexer.Lex(); // Consume the comma.
694
695 StringRef ModifierName;
696 if (ParseIdentifier(ModifierName))
Chris Lattner9a023f72009-06-24 04:43:34 +0000697 return TokError("expected identifier in '.section' directive");
698 Section += ',';
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +0000699 Section += ModifierName;
Chris Lattner9a023f72009-06-24 04:43:34 +0000700 }
701
Daniel Dunbar3f872332009-07-28 16:08:33 +0000702 if (Lexer.isNot(AsmToken::EndOfStatement))
Chris Lattner9a023f72009-06-24 04:43:34 +0000703 return TokError("unexpected token in '.section' directive");
704 Lexer.Lex();
705
Chris Lattner56594f92009-07-31 17:47:16 +0000706 // FIXME: Arch specific.
707 MCSection *S = Ctx.GetSection(Section);
708 if (S == 0)
Chris Lattner0aac3012009-08-08 22:38:48 +0000709 S = MCSectionCOFF::Create(Section, false, SectionKind(), Ctx);
Chris Lattner56594f92009-07-31 17:47:16 +0000710
711 Out.SwitchSection(S);
Chris Lattner9a023f72009-06-24 04:43:34 +0000712 return false;
713}
714
Chris Lattner529fb542009-06-24 05:13:15 +0000715bool AsmParser::ParseDirectiveSectionSwitch(const char *Section,
716 const char *Directives) {
Daniel Dunbar3f872332009-07-28 16:08:33 +0000717 if (Lexer.isNot(AsmToken::EndOfStatement))
Chris Lattner529fb542009-06-24 05:13:15 +0000718 return TokError("unexpected token in section switching directive");
719 Lexer.Lex();
720
721 std::string SectionStr = Section;
722 if (Directives && Directives[0]) {
723 SectionStr += ",";
724 SectionStr += Directives;
725 }
726
Chris Lattner56594f92009-07-31 17:47:16 +0000727 // FIXME: Arch specific.
728 MCSection *S = Ctx.GetSection(Section);
729 if (S == 0)
Chris Lattner0aac3012009-08-08 22:38:48 +0000730 S = MCSectionCOFF::Create(Section, false, SectionKind(), Ctx);
Chris Lattner56594f92009-07-31 17:47:16 +0000731
732 Out.SwitchSection(S);
Chris Lattner529fb542009-06-24 05:13:15 +0000733 return false;
734}
Daniel Dunbara0d14262009-06-24 23:30:00 +0000735
736/// ParseDirectiveAscii:
Daniel Dunbar475839e2009-06-29 20:37:27 +0000737/// ::= ( .ascii | .asciz ) [ "string" ( , "string" )* ]
Daniel Dunbara0d14262009-06-24 23:30:00 +0000738bool AsmParser::ParseDirectiveAscii(bool ZeroTerminated) {
Daniel Dunbar3f872332009-07-28 16:08:33 +0000739 if (Lexer.isNot(AsmToken::EndOfStatement)) {
Daniel Dunbara0d14262009-06-24 23:30:00 +0000740 for (;;) {
Daniel Dunbar3f872332009-07-28 16:08:33 +0000741 if (Lexer.isNot(AsmToken::String))
Daniel Dunbara0d14262009-06-24 23:30:00 +0000742 return TokError("expected string in '.ascii' or '.asciz' directive");
743
744 // FIXME: This shouldn't use a const char* + strlen, the string could have
745 // embedded nulls.
746 // FIXME: Should have accessor for getting string contents.
Daniel Dunbar419aded2009-07-28 16:38:40 +0000747 StringRef Str = Lexer.getTok().getString();
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000748 Out.EmitBytes(Str.substr(1, Str.size() - 2));
Daniel Dunbara0d14262009-06-24 23:30:00 +0000749 if (ZeroTerminated)
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000750 Out.EmitBytes(StringRef("\0", 1));
Daniel Dunbara0d14262009-06-24 23:30:00 +0000751
752 Lexer.Lex();
753
Daniel Dunbar3f872332009-07-28 16:08:33 +0000754 if (Lexer.is(AsmToken::EndOfStatement))
Daniel Dunbara0d14262009-06-24 23:30:00 +0000755 break;
756
Daniel Dunbar3f872332009-07-28 16:08:33 +0000757 if (Lexer.isNot(AsmToken::Comma))
Daniel Dunbara0d14262009-06-24 23:30:00 +0000758 return TokError("unexpected token in '.ascii' or '.asciz' directive");
759 Lexer.Lex();
760 }
761 }
762
763 Lexer.Lex();
764 return false;
765}
766
767/// ParseDirectiveValue
768/// ::= (.byte | .short | ... ) [ expression (, expression)* ]
769bool AsmParser::ParseDirectiveValue(unsigned Size) {
Daniel Dunbar3f872332009-07-28 16:08:33 +0000770 if (Lexer.isNot(AsmToken::EndOfStatement)) {
Daniel Dunbara0d14262009-06-24 23:30:00 +0000771 for (;;) {
Daniel Dunbarf4b830f2009-06-30 02:10:03 +0000772 MCValue Expr;
773 if (ParseRelocatableExpression(Expr))
Daniel Dunbara0d14262009-06-24 23:30:00 +0000774 return true;
775
Daniel Dunbarf4b830f2009-06-30 02:10:03 +0000776 Out.EmitValue(Expr, Size);
Daniel Dunbara0d14262009-06-24 23:30:00 +0000777
Daniel Dunbar3f872332009-07-28 16:08:33 +0000778 if (Lexer.is(AsmToken::EndOfStatement))
Daniel Dunbara0d14262009-06-24 23:30:00 +0000779 break;
780
781 // FIXME: Improve diagnostic.
Daniel Dunbar3f872332009-07-28 16:08:33 +0000782 if (Lexer.isNot(AsmToken::Comma))
Daniel Dunbara0d14262009-06-24 23:30:00 +0000783 return TokError("unexpected token in directive");
784 Lexer.Lex();
785 }
786 }
787
788 Lexer.Lex();
789 return false;
790}
791
792/// ParseDirectiveSpace
793/// ::= .space expression [ , expression ]
794bool AsmParser::ParseDirectiveSpace() {
795 int64_t NumBytes;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000796 if (ParseAbsoluteExpression(NumBytes))
Daniel Dunbara0d14262009-06-24 23:30:00 +0000797 return true;
798
799 int64_t FillExpr = 0;
800 bool HasFillExpr = false;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000801 if (Lexer.isNot(AsmToken::EndOfStatement)) {
802 if (Lexer.isNot(AsmToken::Comma))
Daniel Dunbara0d14262009-06-24 23:30:00 +0000803 return TokError("unexpected token in '.space' directive");
804 Lexer.Lex();
805
Daniel Dunbar475839e2009-06-29 20:37:27 +0000806 if (ParseAbsoluteExpression(FillExpr))
Daniel Dunbara0d14262009-06-24 23:30:00 +0000807 return true;
808
809 HasFillExpr = true;
810
Daniel Dunbar3f872332009-07-28 16:08:33 +0000811 if (Lexer.isNot(AsmToken::EndOfStatement))
Daniel Dunbara0d14262009-06-24 23:30:00 +0000812 return TokError("unexpected token in '.space' directive");
813 }
814
815 Lexer.Lex();
816
817 if (NumBytes <= 0)
818 return TokError("invalid number of bytes in '.space' directive");
819
820 // FIXME: Sometimes the fill expr is 'nop' if it isn't supplied, instead of 0.
821 for (uint64_t i = 0, e = NumBytes; i != e; ++i)
822 Out.EmitValue(MCValue::get(FillExpr), 1);
823
824 return false;
825}
826
827/// ParseDirectiveFill
828/// ::= .fill expression , expression , expression
829bool AsmParser::ParseDirectiveFill() {
830 int64_t NumValues;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000831 if (ParseAbsoluteExpression(NumValues))
Daniel Dunbara0d14262009-06-24 23:30:00 +0000832 return true;
833
Daniel Dunbar3f872332009-07-28 16:08:33 +0000834 if (Lexer.isNot(AsmToken::Comma))
Daniel Dunbara0d14262009-06-24 23:30:00 +0000835 return TokError("unexpected token in '.fill' directive");
836 Lexer.Lex();
837
838 int64_t FillSize;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000839 if (ParseAbsoluteExpression(FillSize))
Daniel Dunbara0d14262009-06-24 23:30:00 +0000840 return true;
841
Daniel Dunbar3f872332009-07-28 16:08:33 +0000842 if (Lexer.isNot(AsmToken::Comma))
Daniel Dunbara0d14262009-06-24 23:30:00 +0000843 return TokError("unexpected token in '.fill' directive");
844 Lexer.Lex();
845
846 int64_t FillExpr;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000847 if (ParseAbsoluteExpression(FillExpr))
Daniel Dunbara0d14262009-06-24 23:30:00 +0000848 return true;
849
Daniel Dunbar3f872332009-07-28 16:08:33 +0000850 if (Lexer.isNot(AsmToken::EndOfStatement))
Daniel Dunbara0d14262009-06-24 23:30:00 +0000851 return TokError("unexpected token in '.fill' directive");
852
853 Lexer.Lex();
854
855 if (FillSize != 1 && FillSize != 2 && FillSize != 4)
856 return TokError("invalid '.fill' size, expected 1, 2, or 4");
857
858 for (uint64_t i = 0, e = NumValues; i != e; ++i)
859 Out.EmitValue(MCValue::get(FillExpr), FillSize);
860
861 return false;
862}
Daniel Dunbarc238b582009-06-25 22:44:51 +0000863
864/// ParseDirectiveOrg
865/// ::= .org expression [ , expression ]
866bool AsmParser::ParseDirectiveOrg() {
Daniel Dunbarf4b830f2009-06-30 02:10:03 +0000867 MCValue Offset;
868 if (ParseRelocatableExpression(Offset))
Daniel Dunbarc238b582009-06-25 22:44:51 +0000869 return true;
870
871 // Parse optional fill expression.
872 int64_t FillExpr = 0;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000873 if (Lexer.isNot(AsmToken::EndOfStatement)) {
874 if (Lexer.isNot(AsmToken::Comma))
Daniel Dunbarc238b582009-06-25 22:44:51 +0000875 return TokError("unexpected token in '.org' directive");
876 Lexer.Lex();
877
Daniel Dunbar475839e2009-06-29 20:37:27 +0000878 if (ParseAbsoluteExpression(FillExpr))
Daniel Dunbarc238b582009-06-25 22:44:51 +0000879 return true;
880
Daniel Dunbar3f872332009-07-28 16:08:33 +0000881 if (Lexer.isNot(AsmToken::EndOfStatement))
Daniel Dunbarc238b582009-06-25 22:44:51 +0000882 return TokError("unexpected token in '.org' directive");
883 }
884
885 Lexer.Lex();
Daniel Dunbarf4b830f2009-06-30 02:10:03 +0000886
887 // FIXME: Only limited forms of relocatable expressions are accepted here, it
888 // has to be relative to the current section.
889 Out.EmitValueToOffset(Offset, FillExpr);
Daniel Dunbarc238b582009-06-25 22:44:51 +0000890
891 return false;
892}
Daniel Dunbarc29dfa72009-06-29 23:46:59 +0000893
894/// ParseDirectiveAlign
895/// ::= {.align, ...} expression [ , expression [ , expression ]]
896bool AsmParser::ParseDirectiveAlign(bool IsPow2, unsigned ValueSize) {
897 int64_t Alignment;
898 if (ParseAbsoluteExpression(Alignment))
899 return true;
900
901 SMLoc MaxBytesLoc;
902 bool HasFillExpr = false;
903 int64_t FillExpr = 0;
904 int64_t MaxBytesToFill = 0;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000905 if (Lexer.isNot(AsmToken::EndOfStatement)) {
906 if (Lexer.isNot(AsmToken::Comma))
Daniel Dunbarc29dfa72009-06-29 23:46:59 +0000907 return TokError("unexpected token in directive");
908 Lexer.Lex();
909
910 // The fill expression can be omitted while specifying a maximum number of
911 // alignment bytes, e.g:
912 // .align 3,,4
Daniel Dunbar3f872332009-07-28 16:08:33 +0000913 if (Lexer.isNot(AsmToken::Comma)) {
Daniel Dunbarc29dfa72009-06-29 23:46:59 +0000914 HasFillExpr = true;
915 if (ParseAbsoluteExpression(FillExpr))
916 return true;
917 }
918
Daniel Dunbar3f872332009-07-28 16:08:33 +0000919 if (Lexer.isNot(AsmToken::EndOfStatement)) {
920 if (Lexer.isNot(AsmToken::Comma))
Daniel Dunbarc29dfa72009-06-29 23:46:59 +0000921 return TokError("unexpected token in directive");
922 Lexer.Lex();
923
924 MaxBytesLoc = Lexer.getLoc();
925 if (ParseAbsoluteExpression(MaxBytesToFill))
926 return true;
927
Daniel Dunbar3f872332009-07-28 16:08:33 +0000928 if (Lexer.isNot(AsmToken::EndOfStatement))
Daniel Dunbarc29dfa72009-06-29 23:46:59 +0000929 return TokError("unexpected token in directive");
930 }
931 }
932
933 Lexer.Lex();
934
935 if (!HasFillExpr) {
936 // FIXME: Sometimes fill with nop.
937 FillExpr = 0;
938 }
939
940 // Compute alignment in bytes.
941 if (IsPow2) {
942 // FIXME: Diagnose overflow.
Chris Lattner39750252009-07-11 22:32:37 +0000943 Alignment = 1LL << Alignment;
Daniel Dunbarc29dfa72009-06-29 23:46:59 +0000944 }
945
946 // Diagnose non-sensical max bytes to fill.
947 if (MaxBytesLoc.isValid()) {
948 if (MaxBytesToFill < 1) {
Daniel Dunbar3fb76832009-06-30 00:49:23 +0000949 Warning(MaxBytesLoc, "alignment directive can never be satisfied in this "
950 "many bytes, ignoring");
Daniel Dunbarc29dfa72009-06-29 23:46:59 +0000951 return false;
952 }
953
954 if (MaxBytesToFill >= Alignment) {
Daniel Dunbar3fb76832009-06-30 00:49:23 +0000955 Warning(MaxBytesLoc, "maximum bytes expression exceeds alignment and "
956 "has no effect");
Daniel Dunbarc29dfa72009-06-29 23:46:59 +0000957 MaxBytesToFill = 0;
958 }
959 }
960
961 // FIXME: Target specific behavior about how the "extra" bytes are filled.
962 Out.EmitValueToAlignment(Alignment, FillExpr, ValueSize, MaxBytesToFill);
963
964 return false;
965}
966
Daniel Dunbard7b267b2009-06-30 00:33:19 +0000967/// ParseDirectiveSymbolAttribute
968/// ::= { ".globl", ".weak", ... } [ identifier ( , identifier )* ]
969bool AsmParser::ParseDirectiveSymbolAttribute(MCStreamer::SymbolAttr Attr) {
Daniel Dunbar3f872332009-07-28 16:08:33 +0000970 if (Lexer.isNot(AsmToken::EndOfStatement)) {
Daniel Dunbard7b267b2009-06-30 00:33:19 +0000971 for (;;) {
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +0000972 StringRef Name;
973
974 if (ParseIdentifier(Name))
Daniel Dunbard7b267b2009-06-30 00:33:19 +0000975 return TokError("expected identifier in directive");
976
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +0000977 MCSymbol *Sym = Ctx.GetOrCreateSymbol(Name);
Daniel Dunbard7b267b2009-06-30 00:33:19 +0000978
979 // If this is use of an undefined symbol then mark it external.
980 if (!Sym->getSection() && !Ctx.GetSymbolValue(Sym))
981 Sym->setExternal(true);
982
983 Out.EmitSymbolAttribute(Sym, Attr);
984
Daniel Dunbar3f872332009-07-28 16:08:33 +0000985 if (Lexer.is(AsmToken::EndOfStatement))
Daniel Dunbard7b267b2009-06-30 00:33:19 +0000986 break;
987
Daniel Dunbar3f872332009-07-28 16:08:33 +0000988 if (Lexer.isNot(AsmToken::Comma))
Daniel Dunbard7b267b2009-06-30 00:33:19 +0000989 return TokError("unexpected token in directive");
990 Lexer.Lex();
991 }
992 }
993
994 Lexer.Lex();
995 return false;
996}
Chris Lattner4e4db7a2009-07-07 20:30:46 +0000997
Kevin Enderby95cf30c2009-07-14 18:17:10 +0000998/// ParseDirectiveDarwinSymbolDesc
999/// ::= .desc identifier , expression
1000bool AsmParser::ParseDirectiveDarwinSymbolDesc() {
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00001001 StringRef Name;
1002 if (ParseIdentifier(Name))
Kevin Enderby95cf30c2009-07-14 18:17:10 +00001003 return TokError("expected identifier in directive");
1004
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00001005 // Handle the identifier as the key symbol.
1006 MCSymbol *Sym = Ctx.GetOrCreateSymbol(Name);
Kevin Enderby95cf30c2009-07-14 18:17:10 +00001007
Daniel Dunbar3f872332009-07-28 16:08:33 +00001008 if (Lexer.isNot(AsmToken::Comma))
Kevin Enderby95cf30c2009-07-14 18:17:10 +00001009 return TokError("unexpected token in '.desc' directive");
1010 Lexer.Lex();
1011
1012 SMLoc DescLoc = Lexer.getLoc();
1013 int64_t DescValue;
1014 if (ParseAbsoluteExpression(DescValue))
1015 return true;
1016
Daniel Dunbar3f872332009-07-28 16:08:33 +00001017 if (Lexer.isNot(AsmToken::EndOfStatement))
Kevin Enderby95cf30c2009-07-14 18:17:10 +00001018 return TokError("unexpected token in '.desc' directive");
1019
1020 Lexer.Lex();
1021
1022 // Set the n_desc field of this Symbol to this DescValue
1023 Out.EmitSymbolDesc(Sym, DescValue);
1024
1025 return false;
1026}
1027
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001028/// ParseDirectiveComm
Chris Lattner1fc3d752009-07-09 17:25:12 +00001029/// ::= ( .comm | .lcomm ) identifier , size_expression [ , align_expression ]
1030bool AsmParser::ParseDirectiveComm(bool IsLocal) {
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00001031 SMLoc IDLoc = Lexer.getLoc();
1032 StringRef Name;
1033 if (ParseIdentifier(Name))
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001034 return TokError("expected identifier in directive");
1035
Daniel Dunbar76c4d762009-07-31 21:55:09 +00001036 // Handle the identifier as the key symbol.
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00001037 MCSymbol *Sym = Ctx.GetOrCreateSymbol(Name);
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001038
Daniel Dunbar3f872332009-07-28 16:08:33 +00001039 if (Lexer.isNot(AsmToken::Comma))
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001040 return TokError("unexpected token in directive");
1041 Lexer.Lex();
1042
1043 int64_t Size;
1044 SMLoc SizeLoc = Lexer.getLoc();
1045 if (ParseAbsoluteExpression(Size))
1046 return true;
1047
1048 int64_t Pow2Alignment = 0;
1049 SMLoc Pow2AlignmentLoc;
Daniel Dunbar3f872332009-07-28 16:08:33 +00001050 if (Lexer.is(AsmToken::Comma)) {
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001051 Lexer.Lex();
1052 Pow2AlignmentLoc = Lexer.getLoc();
1053 if (ParseAbsoluteExpression(Pow2Alignment))
1054 return true;
1055 }
1056
Daniel Dunbar3f872332009-07-28 16:08:33 +00001057 if (Lexer.isNot(AsmToken::EndOfStatement))
Chris Lattner1fc3d752009-07-09 17:25:12 +00001058 return TokError("unexpected token in '.comm' or '.lcomm' directive");
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001059
1060 Lexer.Lex();
1061
Chris Lattner1fc3d752009-07-09 17:25:12 +00001062 // NOTE: a size of zero for a .comm should create a undefined symbol
1063 // but a size of .lcomm creates a bss symbol of size zero.
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001064 if (Size < 0)
Chris Lattner1fc3d752009-07-09 17:25:12 +00001065 return Error(SizeLoc, "invalid '.comm' or '.lcomm' directive size, can't "
1066 "be less than zero");
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001067
1068 // NOTE: The alignment in the directive is a power of 2 value, the assember
1069 // may internally end up wanting an alignment in bytes.
1070 // FIXME: Diagnose overflow.
1071 if (Pow2Alignment < 0)
Chris Lattner1fc3d752009-07-09 17:25:12 +00001072 return Error(Pow2AlignmentLoc, "invalid '.comm' or '.lcomm' directive "
1073 "alignment, can't be less than zero");
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001074
1075 // TODO: Symbol must be undefined or it is a error to re-defined the symbol
1076 if (Sym->getSection() || Ctx.GetSymbolValue(Sym))
1077 return Error(IDLoc, "invalid symbol redefinition");
1078
Chris Lattner1fc3d752009-07-09 17:25:12 +00001079 // Create the Symbol as a common or local common with Size and Pow2Alignment
1080 Out.EmitCommonSymbol(Sym, Size, Pow2Alignment, IsLocal);
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001081
1082 return false;
1083}
Chris Lattner9be3fee2009-07-10 22:20:30 +00001084
1085/// ParseDirectiveDarwinZerofill
1086/// ::= .zerofill segname , sectname [, identifier , size_expression [
1087/// , align_expression ]]
1088bool AsmParser::ParseDirectiveDarwinZerofill() {
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00001089 // FIXME: Handle quoted names here.
1090
Daniel Dunbar3f872332009-07-28 16:08:33 +00001091 if (Lexer.isNot(AsmToken::Identifier))
Chris Lattner9be3fee2009-07-10 22:20:30 +00001092 return TokError("expected segment name after '.zerofill' directive");
Daniel Dunbar419aded2009-07-28 16:38:40 +00001093 std::string Section = Lexer.getTok().getString();
Chris Lattner9be3fee2009-07-10 22:20:30 +00001094 Lexer.Lex();
1095
Daniel Dunbar3f872332009-07-28 16:08:33 +00001096 if (Lexer.isNot(AsmToken::Comma))
Chris Lattner9be3fee2009-07-10 22:20:30 +00001097 return TokError("unexpected token in directive");
1098 Section += ',';
1099 Lexer.Lex();
1100
Daniel Dunbar3f872332009-07-28 16:08:33 +00001101 if (Lexer.isNot(AsmToken::Identifier))
Chris Lattner9be3fee2009-07-10 22:20:30 +00001102 return TokError("expected section name after comma in '.zerofill' "
1103 "directive");
Daniel Dunbar419aded2009-07-28 16:38:40 +00001104 Section += Lexer.getTok().getString().str();
Chris Lattner9be3fee2009-07-10 22:20:30 +00001105 Lexer.Lex();
1106
1107 // FIXME: we will need to tell GetSection() that this is to be created with or
1108 // must have the Mach-O section type of S_ZEROFILL. Something like the code
1109 // below could be done but for now it is not as EmitZerofill() does not know
1110 // how to deal with a section type in the section name like
1111 // ParseDirectiveDarwinSection() allows.
1112 // Section += ',';
1113 // Section += "zerofill";
1114
1115 // If this is the end of the line all that was wanted was to create the
1116 // the section but with no symbol.
Daniel Dunbar3f872332009-07-28 16:08:33 +00001117 if (Lexer.is(AsmToken::EndOfStatement)) {
Chris Lattner6bdd74c2009-07-31 18:27:48 +00001118 // FIXME: Arch specific.
1119 MCSection *S = Ctx.GetSection(Section);
1120 if (S == 0)
Chris Lattner0aac3012009-08-08 22:38:48 +00001121 S = MCSectionCOFF::Create(Section, false, SectionKind(), Ctx);
Chris Lattner6bdd74c2009-07-31 18:27:48 +00001122
Chris Lattner9be3fee2009-07-10 22:20:30 +00001123 // Create the zerofill section but no symbol
Chris Lattner6bdd74c2009-07-31 18:27:48 +00001124 Out.EmitZerofill(S);
Chris Lattner9be3fee2009-07-10 22:20:30 +00001125 return false;
1126 }
1127
Daniel Dunbar3f872332009-07-28 16:08:33 +00001128 if (Lexer.isNot(AsmToken::Comma))
Chris Lattner9be3fee2009-07-10 22:20:30 +00001129 return TokError("unexpected token in directive");
1130 Lexer.Lex();
1131
Daniel Dunbar3f872332009-07-28 16:08:33 +00001132 if (Lexer.isNot(AsmToken::Identifier))
Chris Lattner9be3fee2009-07-10 22:20:30 +00001133 return TokError("expected identifier in directive");
1134
1135 // handle the identifier as the key symbol.
1136 SMLoc IDLoc = Lexer.getLoc();
Daniel Dunbar419aded2009-07-28 16:38:40 +00001137 MCSymbol *Sym = Ctx.GetOrCreateSymbol(Lexer.getTok().getString());
Chris Lattner9be3fee2009-07-10 22:20:30 +00001138 Lexer.Lex();
1139
Daniel Dunbar3f872332009-07-28 16:08:33 +00001140 if (Lexer.isNot(AsmToken::Comma))
Chris Lattner9be3fee2009-07-10 22:20:30 +00001141 return TokError("unexpected token in directive");
1142 Lexer.Lex();
1143
1144 int64_t Size;
1145 SMLoc SizeLoc = Lexer.getLoc();
1146 if (ParseAbsoluteExpression(Size))
1147 return true;
1148
1149 int64_t Pow2Alignment = 0;
1150 SMLoc Pow2AlignmentLoc;
Daniel Dunbar3f872332009-07-28 16:08:33 +00001151 if (Lexer.is(AsmToken::Comma)) {
Chris Lattner9be3fee2009-07-10 22:20:30 +00001152 Lexer.Lex();
1153 Pow2AlignmentLoc = Lexer.getLoc();
1154 if (ParseAbsoluteExpression(Pow2Alignment))
1155 return true;
1156 }
1157
Daniel Dunbar3f872332009-07-28 16:08:33 +00001158 if (Lexer.isNot(AsmToken::EndOfStatement))
Chris Lattner9be3fee2009-07-10 22:20:30 +00001159 return TokError("unexpected token in '.zerofill' directive");
1160
1161 Lexer.Lex();
1162
1163 if (Size < 0)
1164 return Error(SizeLoc, "invalid '.zerofill' directive size, can't be less "
1165 "than zero");
1166
1167 // NOTE: The alignment in the directive is a power of 2 value, the assember
1168 // may internally end up wanting an alignment in bytes.
1169 // FIXME: Diagnose overflow.
1170 if (Pow2Alignment < 0)
1171 return Error(Pow2AlignmentLoc, "invalid '.zerofill' directive alignment, "
1172 "can't be less than zero");
1173
1174 // TODO: Symbol must be undefined or it is a error to re-defined the symbol
1175 if (Sym->getSection() || Ctx.GetSymbolValue(Sym))
1176 return Error(IDLoc, "invalid symbol redefinition");
1177
Chris Lattner6bdd74c2009-07-31 18:27:48 +00001178 // FIXME: Arch specific.
1179 MCSection *S = Ctx.GetSection(Section);
1180 if (S == 0)
Chris Lattner0aac3012009-08-08 22:38:48 +00001181 S = MCSectionCOFF::Create(Section, false, SectionKind(), Ctx);
Chris Lattner6bdd74c2009-07-31 18:27:48 +00001182
Chris Lattner9be3fee2009-07-10 22:20:30 +00001183 // Create the zerofill Symbol with Size and Pow2Alignment
Chris Lattner6bdd74c2009-07-31 18:27:48 +00001184 Out.EmitZerofill(S, Sym, Size, Pow2Alignment);
Chris Lattner9be3fee2009-07-10 22:20:30 +00001185
1186 return false;
1187}
Kevin Enderbya5c78322009-07-13 21:03:15 +00001188
1189/// ParseDirectiveDarwinSubsectionsViaSymbols
1190/// ::= .subsections_via_symbols
1191bool AsmParser::ParseDirectiveDarwinSubsectionsViaSymbols() {
Daniel Dunbar3f872332009-07-28 16:08:33 +00001192 if (Lexer.isNot(AsmToken::EndOfStatement))
Kevin Enderbya5c78322009-07-13 21:03:15 +00001193 return TokError("unexpected token in '.subsections_via_symbols' directive");
1194
1195 Lexer.Lex();
1196
Kevin Enderbyf96db462009-07-16 17:56:39 +00001197 Out.EmitAssemblerFlag(MCStreamer::SubsectionsViaSymbols);
Kevin Enderbya5c78322009-07-13 21:03:15 +00001198
1199 return false;
1200}
Kevin Enderby5f1f0b82009-07-13 23:15:14 +00001201
1202/// ParseDirectiveAbort
1203/// ::= .abort [ "abort_string" ]
1204bool AsmParser::ParseDirectiveAbort() {
Daniel Dunbarf9507ff2009-07-27 23:20:52 +00001205 // FIXME: Use loc from directive.
1206 SMLoc Loc = Lexer.getLoc();
1207
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001208 StringRef Str = "";
Daniel Dunbar3f872332009-07-28 16:08:33 +00001209 if (Lexer.isNot(AsmToken::EndOfStatement)) {
1210 if (Lexer.isNot(AsmToken::String))
Kevin Enderby5f1f0b82009-07-13 23:15:14 +00001211 return TokError("expected string in '.abort' directive");
1212
Daniel Dunbar419aded2009-07-28 16:38:40 +00001213 Str = Lexer.getTok().getString();
Kevin Enderby5f1f0b82009-07-13 23:15:14 +00001214
1215 Lexer.Lex();
1216 }
1217
Daniel Dunbar3f872332009-07-28 16:08:33 +00001218 if (Lexer.isNot(AsmToken::EndOfStatement))
Kevin Enderby5f1f0b82009-07-13 23:15:14 +00001219 return TokError("unexpected token in '.abort' directive");
1220
1221 Lexer.Lex();
1222
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001223 // FIXME: Handle here.
Daniel Dunbarf9507ff2009-07-27 23:20:52 +00001224 if (Str.empty())
1225 Error(Loc, ".abort detected. Assembly stopping.");
1226 else
1227 Error(Loc, ".abort '" + Str + "' detected. Assembly stopping.");
Kevin Enderby5f1f0b82009-07-13 23:15:14 +00001228
1229 return false;
1230}
Kevin Enderby71148242009-07-14 21:35:03 +00001231
1232/// ParseDirectiveLsym
1233/// ::= .lsym identifier , expression
1234bool AsmParser::ParseDirectiveDarwinLsym() {
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00001235 StringRef Name;
1236 if (ParseIdentifier(Name))
Kevin Enderby71148242009-07-14 21:35:03 +00001237 return TokError("expected identifier in directive");
1238
Daniel Dunbar76c4d762009-07-31 21:55:09 +00001239 // Handle the identifier as the key symbol.
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00001240 MCSymbol *Sym = Ctx.GetOrCreateSymbol(Name);
Kevin Enderby71148242009-07-14 21:35:03 +00001241
Daniel Dunbar3f872332009-07-28 16:08:33 +00001242 if (Lexer.isNot(AsmToken::Comma))
Kevin Enderby71148242009-07-14 21:35:03 +00001243 return TokError("unexpected token in '.lsym' directive");
1244 Lexer.Lex();
1245
1246 MCValue Expr;
1247 if (ParseRelocatableExpression(Expr))
1248 return true;
1249
Daniel Dunbar3f872332009-07-28 16:08:33 +00001250 if (Lexer.isNot(AsmToken::EndOfStatement))
Kevin Enderby71148242009-07-14 21:35:03 +00001251 return TokError("unexpected token in '.lsym' directive");
1252
1253 Lexer.Lex();
1254
1255 // Create the Sym with the value of the Expr
1256 Out.EmitLocalSymbol(Sym, Expr);
1257
1258 return false;
1259}
Kevin Enderby1f049b22009-07-14 23:21:55 +00001260
1261/// ParseDirectiveInclude
1262/// ::= .include "filename"
1263bool AsmParser::ParseDirectiveInclude() {
Daniel Dunbar3f872332009-07-28 16:08:33 +00001264 if (Lexer.isNot(AsmToken::String))
Kevin Enderby1f049b22009-07-14 23:21:55 +00001265 return TokError("expected string in '.include' directive");
1266
Daniel Dunbar419aded2009-07-28 16:38:40 +00001267 std::string Filename = Lexer.getTok().getString();
Chris Lattner8e25e2d2009-07-16 06:14:39 +00001268 SMLoc IncludeLoc = Lexer.getLoc();
Kevin Enderby1f049b22009-07-14 23:21:55 +00001269 Lexer.Lex();
1270
Daniel Dunbar3f872332009-07-28 16:08:33 +00001271 if (Lexer.isNot(AsmToken::EndOfStatement))
Kevin Enderby1f049b22009-07-14 23:21:55 +00001272 return TokError("unexpected token in '.include' directive");
1273
Chris Lattner8e25e2d2009-07-16 06:14:39 +00001274 // Strip the quotes.
1275 Filename = Filename.substr(1, Filename.size()-2);
1276
1277 // Attempt to switch the lexer to the included file before consuming the end
1278 // of statement to avoid losing it when we switch.
1279 if (Lexer.EnterIncludeFile(Filename)) {
1280 Lexer.PrintMessage(IncludeLoc,
1281 "Could not find include file '" + Filename + "'",
1282 "error");
1283 return true;
1284 }
Kevin Enderby1f049b22009-07-14 23:21:55 +00001285
1286 return false;
1287}
Kevin Enderby6e68cd92009-07-15 15:30:11 +00001288
1289/// ParseDirectiveDarwinDumpOrLoad
1290/// ::= ( .dump | .load ) "filename"
Kevin Enderby5026ae42009-07-20 20:25:37 +00001291bool AsmParser::ParseDirectiveDarwinDumpOrLoad(SMLoc IDLoc, bool IsDump) {
Daniel Dunbar3f872332009-07-28 16:08:33 +00001292 if (Lexer.isNot(AsmToken::String))
Kevin Enderby6e68cd92009-07-15 15:30:11 +00001293 return TokError("expected string in '.dump' or '.load' directive");
1294
Kevin Enderby6e68cd92009-07-15 15:30:11 +00001295 Lexer.Lex();
1296
Daniel Dunbar3f872332009-07-28 16:08:33 +00001297 if (Lexer.isNot(AsmToken::EndOfStatement))
Kevin Enderby6e68cd92009-07-15 15:30:11 +00001298 return TokError("unexpected token in '.dump' or '.load' directive");
1299
1300 Lexer.Lex();
1301
Kevin Enderby5026ae42009-07-20 20:25:37 +00001302 // FIXME: If/when .dump and .load are implemented they will be done in the
1303 // the assembly parser and not have any need for an MCStreamer API.
Kevin Enderby6e68cd92009-07-15 15:30:11 +00001304 if (IsDump)
Kevin Enderby5026ae42009-07-20 20:25:37 +00001305 Warning(IDLoc, "ignoring directive .dump for now");
Kevin Enderby6e68cd92009-07-15 15:30:11 +00001306 else
Kevin Enderby5026ae42009-07-20 20:25:37 +00001307 Warning(IDLoc, "ignoring directive .load for now");
Kevin Enderby6e68cd92009-07-15 15:30:11 +00001308
1309 return false;
1310}
Kevin Enderbyc114ed72009-08-07 22:46:00 +00001311
1312/// ParseDirectiveIf
1313/// ::= .if expression
1314bool AsmParser::ParseDirectiveIf(SMLoc DirectiveLoc) {
1315 // Consume the identifier that was the .if directive
1316 Lexer.Lex();
1317
1318 TheCondStack.push_back(TheCondState);
1319 TheCondState.TheCond = AsmCond::IfCond;
1320 if(TheCondState.Ignore) {
1321 EatToEndOfStatement();
1322 }
1323 else {
1324 int64_t ExprValue;
1325 if (ParseAbsoluteExpression(ExprValue))
1326 return true;
1327
1328 if (Lexer.isNot(AsmToken::EndOfStatement))
1329 return TokError("unexpected token in '.if' directive");
1330
1331 Lexer.Lex();
1332
1333 TheCondState.CondMet = ExprValue;
1334 TheCondState.Ignore = !TheCondState.CondMet;
1335 }
1336
1337 return false;
1338}
1339
1340/// ParseDirectiveElseIf
1341/// ::= .elseif expression
1342bool AsmParser::ParseDirectiveElseIf(SMLoc DirectiveLoc) {
1343 if (TheCondState.TheCond != AsmCond::IfCond &&
1344 TheCondState.TheCond != AsmCond::ElseIfCond)
1345 Error(DirectiveLoc, "Encountered a .elseif that doesn't follow a .if or "
1346 " an .elseif");
1347 TheCondState.TheCond = AsmCond::ElseIfCond;
1348
1349 // Consume the identifier that was the .elseif directive
1350 Lexer.Lex();
1351
1352 bool LastIgnoreState = false;
1353 if (!TheCondStack.empty())
1354 LastIgnoreState = TheCondStack.back().Ignore;
1355 if (LastIgnoreState || TheCondState.CondMet) {
1356 TheCondState.Ignore = true;
1357 EatToEndOfStatement();
1358 }
1359 else {
1360 int64_t ExprValue;
1361 if (ParseAbsoluteExpression(ExprValue))
1362 return true;
1363
1364 if (Lexer.isNot(AsmToken::EndOfStatement))
1365 return TokError("unexpected token in '.elseif' directive");
1366
1367 Lexer.Lex();
1368 TheCondState.CondMet = ExprValue;
1369 TheCondState.Ignore = !TheCondState.CondMet;
1370 }
1371
1372 return false;
1373}
1374
1375/// ParseDirectiveElse
1376/// ::= .else
1377bool AsmParser::ParseDirectiveElse(SMLoc DirectiveLoc) {
1378 // Consume the identifier that was the .else directive
1379 Lexer.Lex();
1380
1381 if (Lexer.isNot(AsmToken::EndOfStatement))
1382 return TokError("unexpected token in '.else' directive");
1383
1384 Lexer.Lex();
1385
1386 if (TheCondState.TheCond != AsmCond::IfCond &&
1387 TheCondState.TheCond != AsmCond::ElseIfCond)
1388 Error(DirectiveLoc, "Encountered a .else that doesn't follow a .if or an "
1389 ".elseif");
1390 TheCondState.TheCond = AsmCond::ElseCond;
1391 bool LastIgnoreState = false;
1392 if (!TheCondStack.empty())
1393 LastIgnoreState = TheCondStack.back().Ignore;
1394 if (LastIgnoreState || TheCondState.CondMet)
1395 TheCondState.Ignore = true;
1396 else
1397 TheCondState.Ignore = false;
1398
1399 return false;
1400}
1401
1402/// ParseDirectiveEndIf
1403/// ::= .endif
1404bool AsmParser::ParseDirectiveEndIf(SMLoc DirectiveLoc) {
1405 // Consume the identifier that was the .endif directive
1406 Lexer.Lex();
1407
1408 if (Lexer.isNot(AsmToken::EndOfStatement))
1409 return TokError("unexpected token in '.endif' directive");
1410
1411 Lexer.Lex();
1412
1413 if ((TheCondState.TheCond == AsmCond::NoCond) ||
1414 TheCondStack.empty())
1415 Error(DirectiveLoc, "Encountered a .endif that doesn't follow a .if or "
1416 ".else");
1417 if (!TheCondStack.empty()) {
1418 TheCondState = TheCondStack.back();
1419 TheCondStack.pop_back();
1420 }
1421
1422 return false;
1423}