blob: 15a7f336391c62cc8f34398bef2870f9f4b0fee9 [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
Chris Lattnerb717fb02009-07-02 21:53:43 +000048 // While we have input, parse each statement.
Daniel Dunbar3f872332009-07-28 16:08:33 +000049 while (Lexer.isNot(AsmToken::Eof)) {
Chris Lattnerb717fb02009-07-02 21:53:43 +000050 if (!ParseStatement()) continue;
51
52 // If we had an error, remember it and recover by skipping to the next line.
53 HadError = true;
54 EatToEndOfStatement();
55 }
56
57 return HadError;
Chris Lattnerb0789ed2009-06-21 20:54:55 +000058}
59
Chris Lattner2cf5f142009-06-22 01:29:09 +000060/// EatToEndOfStatement - Throw away the rest of the line for testing purposes.
61void AsmParser::EatToEndOfStatement() {
Daniel Dunbar3f872332009-07-28 16:08:33 +000062 while (Lexer.isNot(AsmToken::EndOfStatement) &&
63 Lexer.isNot(AsmToken::Eof))
Chris Lattner2cf5f142009-06-22 01:29:09 +000064 Lexer.Lex();
65
66 // Eat EOL.
Daniel Dunbar3f872332009-07-28 16:08:33 +000067 if (Lexer.is(AsmToken::EndOfStatement))
Chris Lattner2cf5f142009-06-22 01:29:09 +000068 Lexer.Lex();
69}
70
Chris Lattnerc4193832009-06-22 05:51:26 +000071
Chris Lattner74ec1a32009-06-22 06:32:03 +000072/// ParseParenExpr - Parse a paren expression and return it.
73/// NOTE: This assumes the leading '(' has already been consumed.
74///
75/// parenexpr ::= expr)
76///
Daniel Dunbar475839e2009-06-29 20:37:27 +000077bool AsmParser::ParseParenExpr(AsmExpr *&Res) {
Chris Lattner74ec1a32009-06-22 06:32:03 +000078 if (ParseExpression(Res)) return true;
Daniel Dunbar3f872332009-07-28 16:08:33 +000079 if (Lexer.isNot(AsmToken::RParen))
Chris Lattner74ec1a32009-06-22 06:32:03 +000080 return TokError("expected ')' in parentheses expression");
81 Lexer.Lex();
82 return false;
83}
Chris Lattnerc4193832009-06-22 05:51:26 +000084
Chris Lattner74ec1a32009-06-22 06:32:03 +000085/// ParsePrimaryExpr - Parse a primary expression and return it.
86/// primaryexpr ::= (parenexpr
87/// primaryexpr ::= symbol
88/// primaryexpr ::= number
89/// primaryexpr ::= ~,+,- primaryexpr
Daniel Dunbar475839e2009-06-29 20:37:27 +000090bool AsmParser::ParsePrimaryExpr(AsmExpr *&Res) {
Chris Lattnerc4193832009-06-22 05:51:26 +000091 switch (Lexer.getKind()) {
92 default:
93 return TokError("unknown token in expression");
Daniel Dunbar3f872332009-07-28 16:08:33 +000094 case AsmToken::Exclaim:
Daniel Dunbar475839e2009-06-29 20:37:27 +000095 Lexer.Lex(); // Eat the operator.
96 if (ParsePrimaryExpr(Res))
97 return true;
98 Res = new AsmUnaryExpr(AsmUnaryExpr::LNot, Res);
99 return false;
Daniel Dunbar76c4d762009-07-31 21:55:09 +0000100 case AsmToken::String:
Daniel Dunbar3f872332009-07-28 16:08:33 +0000101 case AsmToken::Identifier: {
Chris Lattnerc4193832009-06-22 05:51:26 +0000102 // This is a label, this should be parsed as part of an expression, to
Daniel Dunbar475839e2009-06-29 20:37:27 +0000103 // handle things like LFOO+4.
Daniel Dunbar76c4d762009-07-31 21:55:09 +0000104 MCSymbol *Sym = Ctx.GetOrCreateSymbol(Lexer.getTok().getIdentifier());
Daniel Dunbardce0f3c2009-06-29 23:43:14 +0000105
106 // If this is use of an undefined symbol then mark it external.
107 if (!Sym->getSection() && !Ctx.GetSymbolValue(Sym))
108 Sym->setExternal(true);
109
110 Res = new AsmSymbolRefExpr(Sym);
Chris Lattnerc4193832009-06-22 05:51:26 +0000111 Lexer.Lex(); // Eat identifier.
112 return false;
Daniel Dunbardce0f3c2009-06-29 23:43:14 +0000113 }
Daniel Dunbar3f872332009-07-28 16:08:33 +0000114 case AsmToken::Integer:
Daniel Dunbar419aded2009-07-28 16:38:40 +0000115 Res = new AsmConstantExpr(Lexer.getTok().getIntVal());
Daniel Dunbar76c4d762009-07-31 21:55:09 +0000116 Lexer.Lex(); // Eat token.
Chris Lattnerc4193832009-06-22 05:51:26 +0000117 return false;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000118 case AsmToken::LParen:
Chris Lattner74ec1a32009-06-22 06:32:03 +0000119 Lexer.Lex(); // Eat the '('.
120 return ParseParenExpr(Res);
Daniel Dunbar3f872332009-07-28 16:08:33 +0000121 case AsmToken::Minus:
Chris Lattner74ec1a32009-06-22 06:32:03 +0000122 Lexer.Lex(); // Eat the operator.
Daniel Dunbar475839e2009-06-29 20:37:27 +0000123 if (ParsePrimaryExpr(Res))
124 return true;
125 Res = new AsmUnaryExpr(AsmUnaryExpr::Minus, Res);
126 return false;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000127 case AsmToken::Plus:
Daniel Dunbar475839e2009-06-29 20:37:27 +0000128 Lexer.Lex(); // Eat the operator.
129 if (ParsePrimaryExpr(Res))
130 return true;
131 Res = new AsmUnaryExpr(AsmUnaryExpr::Plus, Res);
132 return false;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000133 case AsmToken::Tilde:
Daniel Dunbar475839e2009-06-29 20:37:27 +0000134 Lexer.Lex(); // Eat the operator.
135 if (ParsePrimaryExpr(Res))
136 return true;
137 Res = new AsmUnaryExpr(AsmUnaryExpr::Not, Res);
138 return false;
Chris Lattnerc4193832009-06-22 05:51:26 +0000139 }
140}
Chris Lattner74ec1a32009-06-22 06:32:03 +0000141
142/// ParseExpression - Parse an expression and return it.
143///
144/// expr ::= expr +,- expr -> lowest.
145/// expr ::= expr |,^,&,! expr -> middle.
146/// expr ::= expr *,/,%,<<,>> expr -> highest.
147/// expr ::= primaryexpr
148///
Daniel Dunbar475839e2009-06-29 20:37:27 +0000149bool AsmParser::ParseExpression(AsmExpr *&Res) {
150 Res = 0;
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000151 return ParsePrimaryExpr(Res) ||
152 ParseBinOpRHS(1, Res);
Chris Lattner74ec1a32009-06-22 06:32:03 +0000153}
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000154
Daniel Dunbar475839e2009-06-29 20:37:27 +0000155bool AsmParser::ParseAbsoluteExpression(int64_t &Res) {
156 AsmExpr *Expr;
157
Daniel Dunbarf4b830f2009-06-30 02:10:03 +0000158 SMLoc StartLoc = Lexer.getLoc();
Daniel Dunbar475839e2009-06-29 20:37:27 +0000159 if (ParseExpression(Expr))
160 return true;
161
162 if (!Expr->EvaluateAsAbsolute(Ctx, Res))
Daniel Dunbarf4b830f2009-06-30 02:10:03 +0000163 return Error(StartLoc, "expected absolute expression");
Daniel Dunbar475839e2009-06-29 20:37:27 +0000164
165 return false;
166}
167
Daniel Dunbar15d17072009-06-30 01:49:52 +0000168bool AsmParser::ParseRelocatableExpression(MCValue &Res) {
169 AsmExpr *Expr;
170
Daniel Dunbarf4b830f2009-06-30 02:10:03 +0000171 SMLoc StartLoc = Lexer.getLoc();
Daniel Dunbar15d17072009-06-30 01:49:52 +0000172 if (ParseExpression(Expr))
173 return true;
174
175 if (!Expr->EvaluateAsRelocatable(Ctx, Res))
Daniel Dunbarf4b830f2009-06-30 02:10:03 +0000176 return Error(StartLoc, "expected relocatable expression");
Daniel Dunbar15d17072009-06-30 01:49:52 +0000177
178 return false;
179}
180
Daniel Dunbar2c3f00c2009-07-02 02:09:07 +0000181bool AsmParser::ParseParenRelocatableExpression(MCValue &Res) {
182 AsmExpr *Expr;
183
184 SMLoc StartLoc = Lexer.getLoc();
185 if (ParseParenExpr(Expr))
186 return true;
187
188 if (!Expr->EvaluateAsRelocatable(Ctx, Res))
189 return Error(StartLoc, "expected relocatable expression");
190
191 return false;
192}
193
Daniel Dunbar3f872332009-07-28 16:08:33 +0000194static unsigned getBinOpPrecedence(AsmToken::TokenKind K,
Daniel Dunbar475839e2009-06-29 20:37:27 +0000195 AsmBinaryExpr::Opcode &Kind) {
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000196 switch (K) {
197 default: return 0; // not a binop.
Daniel Dunbar475839e2009-06-29 20:37:27 +0000198
199 // Lowest Precedence: &&, ||
Daniel Dunbar3f872332009-07-28 16:08:33 +0000200 case AsmToken::AmpAmp:
Daniel Dunbar475839e2009-06-29 20:37:27 +0000201 Kind = AsmBinaryExpr::LAnd;
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000202 return 1;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000203 case AsmToken::PipePipe:
Daniel Dunbar475839e2009-06-29 20:37:27 +0000204 Kind = AsmBinaryExpr::LOr;
205 return 1;
206
207 // Low Precedence: +, -, ==, !=, <>, <, <=, >, >=
Daniel Dunbar3f872332009-07-28 16:08:33 +0000208 case AsmToken::Plus:
Daniel Dunbar475839e2009-06-29 20:37:27 +0000209 Kind = AsmBinaryExpr::Add;
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000210 return 2;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000211 case AsmToken::Minus:
Daniel Dunbar475839e2009-06-29 20:37:27 +0000212 Kind = AsmBinaryExpr::Sub;
213 return 2;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000214 case AsmToken::EqualEqual:
Daniel Dunbar475839e2009-06-29 20:37:27 +0000215 Kind = AsmBinaryExpr::EQ;
216 return 2;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000217 case AsmToken::ExclaimEqual:
218 case AsmToken::LessGreater:
Daniel Dunbar475839e2009-06-29 20:37:27 +0000219 Kind = AsmBinaryExpr::NE;
220 return 2;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000221 case AsmToken::Less:
Daniel Dunbar475839e2009-06-29 20:37:27 +0000222 Kind = AsmBinaryExpr::LT;
223 return 2;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000224 case AsmToken::LessEqual:
Daniel Dunbar475839e2009-06-29 20:37:27 +0000225 Kind = AsmBinaryExpr::LTE;
226 return 2;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000227 case AsmToken::Greater:
Daniel Dunbar475839e2009-06-29 20:37:27 +0000228 Kind = AsmBinaryExpr::GT;
229 return 2;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000230 case AsmToken::GreaterEqual:
Daniel Dunbar475839e2009-06-29 20:37:27 +0000231 Kind = AsmBinaryExpr::GTE;
232 return 2;
233
234 // Intermediate Precedence: |, &, ^
235 //
236 // FIXME: gas seems to support '!' as an infix operator?
Daniel Dunbar3f872332009-07-28 16:08:33 +0000237 case AsmToken::Pipe:
Daniel Dunbar475839e2009-06-29 20:37:27 +0000238 Kind = AsmBinaryExpr::Or;
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000239 return 3;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000240 case AsmToken::Caret:
Daniel Dunbar475839e2009-06-29 20:37:27 +0000241 Kind = AsmBinaryExpr::Xor;
242 return 3;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000243 case AsmToken::Amp:
Daniel Dunbar475839e2009-06-29 20:37:27 +0000244 Kind = AsmBinaryExpr::And;
245 return 3;
246
247 // Highest Precedence: *, /, %, <<, >>
Daniel Dunbar3f872332009-07-28 16:08:33 +0000248 case AsmToken::Star:
Daniel Dunbar475839e2009-06-29 20:37:27 +0000249 Kind = AsmBinaryExpr::Mul;
250 return 4;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000251 case AsmToken::Slash:
Daniel Dunbar475839e2009-06-29 20:37:27 +0000252 Kind = AsmBinaryExpr::Div;
253 return 4;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000254 case AsmToken::Percent:
Daniel Dunbar475839e2009-06-29 20:37:27 +0000255 Kind = AsmBinaryExpr::Mod;
256 return 4;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000257 case AsmToken::LessLess:
Daniel Dunbar475839e2009-06-29 20:37:27 +0000258 Kind = AsmBinaryExpr::Shl;
259 return 4;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000260 case AsmToken::GreaterGreater:
Daniel Dunbar475839e2009-06-29 20:37:27 +0000261 Kind = AsmBinaryExpr::Shr;
262 return 4;
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000263 }
264}
265
266
267/// ParseBinOpRHS - Parse all binary operators with precedence >= 'Precedence'.
268/// Res contains the LHS of the expression on input.
Daniel Dunbar475839e2009-06-29 20:37:27 +0000269bool AsmParser::ParseBinOpRHS(unsigned Precedence, AsmExpr *&Res) {
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000270 while (1) {
Daniel Dunbar51330632009-06-29 21:14:21 +0000271 AsmBinaryExpr::Opcode Kind = AsmBinaryExpr::Add;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000272 unsigned TokPrec = getBinOpPrecedence(Lexer.getKind(), Kind);
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000273
274 // If the next token is lower precedence than we are allowed to eat, return
275 // successfully with what we ate already.
276 if (TokPrec < Precedence)
277 return false;
278
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000279 Lexer.Lex();
280
281 // Eat the next primary expression.
Daniel Dunbar475839e2009-06-29 20:37:27 +0000282 AsmExpr *RHS;
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000283 if (ParsePrimaryExpr(RHS)) return true;
284
285 // If BinOp binds less tightly with RHS than the operator after RHS, let
286 // the pending operator take RHS as its LHS.
Daniel Dunbar475839e2009-06-29 20:37:27 +0000287 AsmBinaryExpr::Opcode Dummy;
288 unsigned NextTokPrec = getBinOpPrecedence(Lexer.getKind(), Dummy);
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000289 if (TokPrec < NextTokPrec) {
290 if (ParseBinOpRHS(Precedence+1, RHS)) return true;
291 }
292
Daniel Dunbar475839e2009-06-29 20:37:27 +0000293 // Merge LHS and RHS according to operator.
294 Res = new AsmBinaryExpr(Kind, Res, RHS);
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000295 }
296}
297
Chris Lattnerc4193832009-06-22 05:51:26 +0000298
299
300
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000301/// ParseStatement:
302/// ::= EndOfStatement
Chris Lattner2cf5f142009-06-22 01:29:09 +0000303/// ::= Label* Directive ...Operands... EndOfStatement
304/// ::= Label* Identifier OperandList* EndOfStatement
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000305bool AsmParser::ParseStatement() {
306 switch (Lexer.getKind()) {
307 default:
Chris Lattner14ee48a2009-06-21 21:22:11 +0000308 return TokError("unexpected token at start of statement");
Daniel Dunbar3f872332009-07-28 16:08:33 +0000309 case AsmToken::EndOfStatement:
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000310 Lexer.Lex();
311 return false;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000312 case AsmToken::Identifier:
Daniel Dunbar76c4d762009-07-31 21:55:09 +0000313 case AsmToken::String:
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000314 break;
315 // TODO: Recurse on local labels etc.
316 }
317
318 // If we have an identifier, handle it as the key symbol.
Daniel Dunbar419aded2009-07-28 16:38:40 +0000319 AsmToken ID = Lexer.getTok();
320 SMLoc IDLoc = ID.getLoc();
Daniel Dunbar76c4d762009-07-31 21:55:09 +0000321 StringRef IDVal = ID.getIdentifier();
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000322
323 // Consume the identifier, see what is after it.
Daniel Dunbara3c924f2009-07-28 16:56:42 +0000324 switch (Lexer.Lex().getKind()) {
Daniel Dunbar3f872332009-07-28 16:08:33 +0000325 case AsmToken::Colon: {
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000326 // identifier ':' -> Label.
327 Lexer.Lex();
Daniel Dunbardce0f3c2009-06-29 23:43:14 +0000328
329 // Diagnose attempt to use a variable as a label.
330 //
331 // FIXME: Diagnostics. Note the location of the definition as a label.
332 // FIXME: This doesn't diagnose assignment to a symbol which has been
333 // implicitly marked as external.
334 MCSymbol *Sym = Ctx.GetOrCreateSymbol(IDVal);
335 if (Sym->getSection())
336 return Error(IDLoc, "invalid symbol redefinition");
337 if (Ctx.GetSymbolValue(Sym))
338 return Error(IDLoc, "symbol already used as assembler variable");
Chris Lattnerc69485e2009-06-24 04:31:49 +0000339
340 // Since we saw a label, create a symbol and emit it.
341 // FIXME: If the label starts with L it is an assembler temporary label.
342 // Why does the client of this api need to know this?
Daniel Dunbardce0f3c2009-06-29 23:43:14 +0000343 Out.EmitLabel(Sym);
344
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000345 return ParseStatement();
Daniel Dunbardce0f3c2009-06-29 23:43:14 +0000346 }
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000347
Daniel Dunbar3f872332009-07-28 16:08:33 +0000348 case AsmToken::Equal:
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000349 // identifier '=' ... -> assignment statement
350 Lexer.Lex();
351
352 return ParseAssignment(IDVal, false);
353
354 default: // Normal instruction or directive.
355 break;
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000356 }
357
358 // Otherwise, we have a normal instruction or directive.
Chris Lattner2cf5f142009-06-22 01:29:09 +0000359 if (IDVal[0] == '.') {
Chris Lattner529fb542009-06-24 05:13:15 +0000360 // FIXME: This should be driven based on a hash lookup and callback.
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000361 if (IDVal == ".section")
Chris Lattner529fb542009-06-24 05:13:15 +0000362 return ParseDirectiveDarwinSection();
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000363 if (IDVal == ".text")
Chris Lattner529fb542009-06-24 05:13:15 +0000364 // FIXME: This changes behavior based on the -static flag to the
365 // assembler.
366 return ParseDirectiveSectionSwitch("__TEXT,__text",
367 "regular,pure_instructions");
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000368 if (IDVal == ".const")
Chris Lattner529fb542009-06-24 05:13:15 +0000369 return ParseDirectiveSectionSwitch("__TEXT,__const");
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000370 if (IDVal == ".static_const")
Chris Lattner529fb542009-06-24 05:13:15 +0000371 return ParseDirectiveSectionSwitch("__TEXT,__static_const");
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000372 if (IDVal == ".cstring")
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000373 return ParseDirectiveSectionSwitch("__TEXT,__cstring",
374 "cstring_literals");
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000375 if (IDVal == ".literal4")
Chris Lattner529fb542009-06-24 05:13:15 +0000376 return ParseDirectiveSectionSwitch("__TEXT,__literal4", "4byte_literals");
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000377 if (IDVal == ".literal8")
Chris Lattner529fb542009-06-24 05:13:15 +0000378 return ParseDirectiveSectionSwitch("__TEXT,__literal8", "8byte_literals");
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000379 if (IDVal == ".literal16")
Chris Lattner529fb542009-06-24 05:13:15 +0000380 return ParseDirectiveSectionSwitch("__TEXT,__literal16",
381 "16byte_literals");
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000382 if (IDVal == ".constructor")
Chris Lattner529fb542009-06-24 05:13:15 +0000383 return ParseDirectiveSectionSwitch("__TEXT,__constructor");
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000384 if (IDVal == ".destructor")
Chris Lattner529fb542009-06-24 05:13:15 +0000385 return ParseDirectiveSectionSwitch("__TEXT,__destructor");
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000386 if (IDVal == ".fvmlib_init0")
Chris Lattner529fb542009-06-24 05:13:15 +0000387 return ParseDirectiveSectionSwitch("__TEXT,__fvmlib_init0");
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000388 if (IDVal == ".fvmlib_init1")
Chris Lattner529fb542009-06-24 05:13:15 +0000389 return ParseDirectiveSectionSwitch("__TEXT,__fvmlib_init1");
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000390 if (IDVal == ".symbol_stub") // FIXME: Different on PPC.
Chris Lattner529fb542009-06-24 05:13:15 +0000391 return ParseDirectiveSectionSwitch("__IMPORT,__jump_table,symbol_stubs",
392 "self_modifying_code+pure_instructions,5");
393 // FIXME: .picsymbol_stub on PPC.
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000394 if (IDVal == ".data")
Chris Lattner529fb542009-06-24 05:13:15 +0000395 return ParseDirectiveSectionSwitch("__DATA,__data");
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000396 if (IDVal == ".static_data")
Chris Lattner529fb542009-06-24 05:13:15 +0000397 return ParseDirectiveSectionSwitch("__DATA,__static_data");
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000398 if (IDVal == ".non_lazy_symbol_pointer")
Chris Lattner529fb542009-06-24 05:13:15 +0000399 return ParseDirectiveSectionSwitch("__DATA,__nl_symbol_pointer",
400 "non_lazy_symbol_pointers");
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000401 if (IDVal == ".lazy_symbol_pointer")
Chris Lattner529fb542009-06-24 05:13:15 +0000402 return ParseDirectiveSectionSwitch("__DATA,__la_symbol_pointer",
403 "lazy_symbol_pointers");
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000404 if (IDVal == ".dyld")
Chris Lattner529fb542009-06-24 05:13:15 +0000405 return ParseDirectiveSectionSwitch("__DATA,__dyld");
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000406 if (IDVal == ".mod_init_func")
Chris Lattner529fb542009-06-24 05:13:15 +0000407 return ParseDirectiveSectionSwitch("__DATA,__mod_init_func",
408 "mod_init_funcs");
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000409 if (IDVal == ".mod_term_func")
Chris Lattner529fb542009-06-24 05:13:15 +0000410 return ParseDirectiveSectionSwitch("__DATA,__mod_term_func",
411 "mod_term_funcs");
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000412 if (IDVal == ".const_data")
Chris Lattner529fb542009-06-24 05:13:15 +0000413 return ParseDirectiveSectionSwitch("__DATA,__const", "regular");
414
415
416 // FIXME: Verify attributes on sections.
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000417 if (IDVal == ".objc_class")
Chris Lattner529fb542009-06-24 05:13:15 +0000418 return ParseDirectiveSectionSwitch("__OBJC,__class");
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000419 if (IDVal == ".objc_meta_class")
Chris Lattner529fb542009-06-24 05:13:15 +0000420 return ParseDirectiveSectionSwitch("__OBJC,__meta_class");
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000421 if (IDVal == ".objc_cat_cls_meth")
Chris Lattner529fb542009-06-24 05:13:15 +0000422 return ParseDirectiveSectionSwitch("__OBJC,__cat_cls_meth");
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000423 if (IDVal == ".objc_cat_inst_meth")
Chris Lattner529fb542009-06-24 05:13:15 +0000424 return ParseDirectiveSectionSwitch("__OBJC,__cat_inst_meth");
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000425 if (IDVal == ".objc_protocol")
Chris Lattner529fb542009-06-24 05:13:15 +0000426 return ParseDirectiveSectionSwitch("__OBJC,__protocol");
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000427 if (IDVal == ".objc_string_object")
Chris Lattner529fb542009-06-24 05:13:15 +0000428 return ParseDirectiveSectionSwitch("__OBJC,__string_object");
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000429 if (IDVal == ".objc_cls_meth")
Chris Lattner529fb542009-06-24 05:13:15 +0000430 return ParseDirectiveSectionSwitch("__OBJC,__cls_meth");
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000431 if (IDVal == ".objc_inst_meth")
Chris Lattner529fb542009-06-24 05:13:15 +0000432 return ParseDirectiveSectionSwitch("__OBJC,__inst_meth");
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000433 if (IDVal == ".objc_cls_refs")
Chris Lattner529fb542009-06-24 05:13:15 +0000434 return ParseDirectiveSectionSwitch("__OBJC,__cls_refs");
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000435 if (IDVal == ".objc_message_refs")
Chris Lattner529fb542009-06-24 05:13:15 +0000436 return ParseDirectiveSectionSwitch("__OBJC,__message_refs");
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000437 if (IDVal == ".objc_symbols")
Chris Lattner529fb542009-06-24 05:13:15 +0000438 return ParseDirectiveSectionSwitch("__OBJC,__symbols");
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000439 if (IDVal == ".objc_category")
Chris Lattner529fb542009-06-24 05:13:15 +0000440 return ParseDirectiveSectionSwitch("__OBJC,__category");
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000441 if (IDVal == ".objc_class_vars")
Chris Lattner529fb542009-06-24 05:13:15 +0000442 return ParseDirectiveSectionSwitch("__OBJC,__class_vars");
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000443 if (IDVal == ".objc_instance_vars")
Chris Lattner529fb542009-06-24 05:13:15 +0000444 return ParseDirectiveSectionSwitch("__OBJC,__instance_vars");
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000445 if (IDVal == ".objc_module_info")
Chris Lattner529fb542009-06-24 05:13:15 +0000446 return ParseDirectiveSectionSwitch("__OBJC,__module_info");
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000447 if (IDVal == ".objc_class_names")
Chris Lattner529fb542009-06-24 05:13:15 +0000448 return ParseDirectiveSectionSwitch("__TEXT,__cstring","cstring_literals");
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000449 if (IDVal == ".objc_meth_var_types")
Chris Lattner529fb542009-06-24 05:13:15 +0000450 return ParseDirectiveSectionSwitch("__TEXT,__cstring","cstring_literals");
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000451 if (IDVal == ".objc_meth_var_names")
Chris Lattner529fb542009-06-24 05:13:15 +0000452 return ParseDirectiveSectionSwitch("__TEXT,__cstring","cstring_literals");
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000453 if (IDVal == ".objc_selector_strs")
Chris Lattner529fb542009-06-24 05:13:15 +0000454 return ParseDirectiveSectionSwitch("__OBJC,__selector_strs");
Chris Lattner9a023f72009-06-24 04:43:34 +0000455
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000456 // Assembler features
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000457 if (IDVal == ".set")
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000458 return ParseDirectiveSet();
459
Daniel Dunbara0d14262009-06-24 23:30:00 +0000460 // Data directives
461
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000462 if (IDVal == ".ascii")
Daniel Dunbara0d14262009-06-24 23:30:00 +0000463 return ParseDirectiveAscii(false);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000464 if (IDVal == ".asciz")
Daniel Dunbara0d14262009-06-24 23:30:00 +0000465 return ParseDirectiveAscii(true);
466
467 // FIXME: Target hooks for size? Also for "word", "hword".
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000468 if (IDVal == ".byte")
Daniel Dunbara0d14262009-06-24 23:30:00 +0000469 return ParseDirectiveValue(1);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000470 if (IDVal == ".short")
Daniel Dunbara0d14262009-06-24 23:30:00 +0000471 return ParseDirectiveValue(2);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000472 if (IDVal == ".long")
Daniel Dunbara0d14262009-06-24 23:30:00 +0000473 return ParseDirectiveValue(4);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000474 if (IDVal == ".quad")
Daniel Dunbara0d14262009-06-24 23:30:00 +0000475 return ParseDirectiveValue(8);
Daniel Dunbarc29dfa72009-06-29 23:46:59 +0000476
477 // FIXME: Target hooks for IsPow2.
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000478 if (IDVal == ".align")
Daniel Dunbarc29dfa72009-06-29 23:46:59 +0000479 return ParseDirectiveAlign(/*IsPow2=*/true, /*ExprSize=*/1);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000480 if (IDVal == ".align32")
Daniel Dunbarc29dfa72009-06-29 23:46:59 +0000481 return ParseDirectiveAlign(/*IsPow2=*/true, /*ExprSize=*/4);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000482 if (IDVal == ".balign")
Daniel Dunbarc29dfa72009-06-29 23:46:59 +0000483 return ParseDirectiveAlign(/*IsPow2=*/false, /*ExprSize=*/1);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000484 if (IDVal == ".balignw")
Daniel Dunbarc29dfa72009-06-29 23:46:59 +0000485 return ParseDirectiveAlign(/*IsPow2=*/false, /*ExprSize=*/2);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000486 if (IDVal == ".balignl")
Daniel Dunbarc29dfa72009-06-29 23:46:59 +0000487 return ParseDirectiveAlign(/*IsPow2=*/false, /*ExprSize=*/4);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000488 if (IDVal == ".p2align")
Daniel Dunbarc29dfa72009-06-29 23:46:59 +0000489 return ParseDirectiveAlign(/*IsPow2=*/true, /*ExprSize=*/1);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000490 if (IDVal == ".p2alignw")
Daniel Dunbarc29dfa72009-06-29 23:46:59 +0000491 return ParseDirectiveAlign(/*IsPow2=*/true, /*ExprSize=*/2);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000492 if (IDVal == ".p2alignl")
Daniel Dunbarc29dfa72009-06-29 23:46:59 +0000493 return ParseDirectiveAlign(/*IsPow2=*/true, /*ExprSize=*/4);
494
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000495 if (IDVal == ".org")
Daniel Dunbarc238b582009-06-25 22:44:51 +0000496 return ParseDirectiveOrg();
Daniel Dunbarc29dfa72009-06-29 23:46:59 +0000497
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000498 if (IDVal == ".fill")
Daniel Dunbarc29dfa72009-06-29 23:46:59 +0000499 return ParseDirectiveFill();
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000500 if (IDVal == ".space")
Daniel Dunbara0d14262009-06-24 23:30:00 +0000501 return ParseDirectiveSpace();
502
Daniel Dunbard7b267b2009-06-30 00:33:19 +0000503 // Symbol attribute directives
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000504 if (IDVal == ".globl" || IDVal == ".global")
Daniel Dunbard7b267b2009-06-30 00:33:19 +0000505 return ParseDirectiveSymbolAttribute(MCStreamer::Global);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000506 if (IDVal == ".hidden")
Daniel Dunbard7b267b2009-06-30 00:33:19 +0000507 return ParseDirectiveSymbolAttribute(MCStreamer::Hidden);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000508 if (IDVal == ".indirect_symbol")
Daniel Dunbard7b267b2009-06-30 00:33:19 +0000509 return ParseDirectiveSymbolAttribute(MCStreamer::IndirectSymbol);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000510 if (IDVal == ".internal")
Daniel Dunbard7b267b2009-06-30 00:33:19 +0000511 return ParseDirectiveSymbolAttribute(MCStreamer::Internal);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000512 if (IDVal == ".lazy_reference")
Daniel Dunbard7b267b2009-06-30 00:33:19 +0000513 return ParseDirectiveSymbolAttribute(MCStreamer::LazyReference);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000514 if (IDVal == ".no_dead_strip")
Daniel Dunbard7b267b2009-06-30 00:33:19 +0000515 return ParseDirectiveSymbolAttribute(MCStreamer::NoDeadStrip);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000516 if (IDVal == ".private_extern")
Daniel Dunbard7b267b2009-06-30 00:33:19 +0000517 return ParseDirectiveSymbolAttribute(MCStreamer::PrivateExtern);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000518 if (IDVal == ".protected")
Daniel Dunbard7b267b2009-06-30 00:33:19 +0000519 return ParseDirectiveSymbolAttribute(MCStreamer::Protected);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000520 if (IDVal == ".reference")
Daniel Dunbard7b267b2009-06-30 00:33:19 +0000521 return ParseDirectiveSymbolAttribute(MCStreamer::Reference);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000522 if (IDVal == ".weak")
Daniel Dunbard7b267b2009-06-30 00:33:19 +0000523 return ParseDirectiveSymbolAttribute(MCStreamer::Weak);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000524 if (IDVal == ".weak_definition")
Daniel Dunbard7b267b2009-06-30 00:33:19 +0000525 return ParseDirectiveSymbolAttribute(MCStreamer::WeakDefinition);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000526 if (IDVal == ".weak_reference")
Daniel Dunbard7b267b2009-06-30 00:33:19 +0000527 return ParseDirectiveSymbolAttribute(MCStreamer::WeakReference);
528
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000529 if (IDVal == ".comm")
Chris Lattner1fc3d752009-07-09 17:25:12 +0000530 return ParseDirectiveComm(/*IsLocal=*/false);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000531 if (IDVal == ".lcomm")
Chris Lattner1fc3d752009-07-09 17:25:12 +0000532 return ParseDirectiveComm(/*IsLocal=*/true);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000533 if (IDVal == ".zerofill")
Chris Lattner9be3fee2009-07-10 22:20:30 +0000534 return ParseDirectiveDarwinZerofill();
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000535 if (IDVal == ".desc")
Kevin Enderby95cf30c2009-07-14 18:17:10 +0000536 return ParseDirectiveDarwinSymbolDesc();
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000537 if (IDVal == ".lsym")
Kevin Enderby71148242009-07-14 21:35:03 +0000538 return ParseDirectiveDarwinLsym();
Chris Lattner4e4db7a2009-07-07 20:30:46 +0000539
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000540 if (IDVal == ".subsections_via_symbols")
Kevin Enderbya5c78322009-07-13 21:03:15 +0000541 return ParseDirectiveDarwinSubsectionsViaSymbols();
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000542 if (IDVal == ".abort")
Kevin Enderby5f1f0b82009-07-13 23:15:14 +0000543 return ParseDirectiveAbort();
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000544 if (IDVal == ".include")
Kevin Enderby1f049b22009-07-14 23:21:55 +0000545 return ParseDirectiveInclude();
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000546 if (IDVal == ".dump")
Kevin Enderby5026ae42009-07-20 20:25:37 +0000547 return ParseDirectiveDarwinDumpOrLoad(IDLoc, /*IsDump=*/true);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000548 if (IDVal == ".load")
Kevin Enderby5026ae42009-07-20 20:25:37 +0000549 return ParseDirectiveDarwinDumpOrLoad(IDLoc, /*IsLoad=*/false);
Kevin Enderbya5c78322009-07-13 21:03:15 +0000550
Daniel Dunbar3fb76832009-06-30 00:49:23 +0000551 Warning(IDLoc, "ignoring directive for now");
Chris Lattner2cf5f142009-06-22 01:29:09 +0000552 EatToEndOfStatement();
553 return false;
554 }
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000555
Chris Lattner29dfe7c2009-06-23 18:41:30 +0000556 MCInst Inst;
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000557 if (getTargetParser().ParseInstruction(IDVal, Inst))
Chris Lattner29dfe7c2009-06-23 18:41:30 +0000558 return true;
Chris Lattner2cf5f142009-06-22 01:29:09 +0000559
Daniel Dunbar3f872332009-07-28 16:08:33 +0000560 if (Lexer.isNot(AsmToken::EndOfStatement))
Chris Lattner9a023f72009-06-24 04:43:34 +0000561 return TokError("unexpected token in argument list");
Chris Lattner2cf5f142009-06-22 01:29:09 +0000562
563 // Eat the end of statement marker.
564 Lexer.Lex();
565
566 // Instruction is good, process it.
Daniel Dunbar0eebb052009-07-01 06:35:48 +0000567 Out.EmitInstruction(Inst);
Chris Lattner2cf5f142009-06-22 01:29:09 +0000568
569 // Skip to end of line for now.
Chris Lattner27aa7d22009-06-21 20:16:42 +0000570 return false;
571}
Chris Lattner9a023f72009-06-24 04:43:34 +0000572
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000573bool AsmParser::ParseAssignment(const StringRef &Name, bool IsDotSet) {
Daniel Dunbardce0f3c2009-06-29 23:43:14 +0000574 // FIXME: Use better location, we should use proper tokens.
575 SMLoc EqualLoc = Lexer.getLoc();
576
Daniel Dunbarf4b830f2009-06-30 02:10:03 +0000577 MCValue Value;
578 if (ParseRelocatableExpression(Value))
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000579 return true;
580
Daniel Dunbar3f872332009-07-28 16:08:33 +0000581 if (Lexer.isNot(AsmToken::EndOfStatement))
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000582 return TokError("unexpected token in assignment");
583
584 // Eat the end of statement marker.
585 Lexer.Lex();
586
Daniel Dunbardce0f3c2009-06-29 23:43:14 +0000587 // Diagnose assignment to a label.
588 //
589 // FIXME: Diagnostics. Note the location of the definition as a label.
590 // FIXME: This doesn't diagnose assignment to a symbol which has been
591 // implicitly marked as external.
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000592 // FIXME: Handle '.'.
Daniel Dunbardce0f3c2009-06-29 23:43:14 +0000593 // FIXME: Diagnose assignment to protected identifier (e.g., register name).
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000594 MCSymbol *Sym = Ctx.GetOrCreateSymbol(Name);
Daniel Dunbardce0f3c2009-06-29 23:43:14 +0000595 if (Sym->getSection())
596 return Error(EqualLoc, "invalid assignment to symbol emitted as a label");
597 if (Sym->isExternal())
598 return Error(EqualLoc, "invalid assignment to external symbol");
599
600 // Do the assignment.
Daniel Dunbarf4b830f2009-06-30 02:10:03 +0000601 Out.EmitAssignment(Sym, Value, IsDotSet);
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000602
603 return false;
604}
605
606/// ParseDirectiveSet:
607/// ::= .set identifier ',' expression
608bool AsmParser::ParseDirectiveSet() {
Daniel Dunbar3f872332009-07-28 16:08:33 +0000609 if (Lexer.isNot(AsmToken::Identifier))
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000610 return TokError("expected identifier after '.set' directive");
611
Daniel Dunbar419aded2009-07-28 16:38:40 +0000612 StringRef Name = Lexer.getTok().getString();
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000613
Daniel Dunbara3c924f2009-07-28 16:56:42 +0000614 if (Lexer.Lex().isNot(AsmToken::Comma))
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000615 return TokError("unexpected token in '.set'");
616 Lexer.Lex();
617
618 return ParseAssignment(Name, true);
619}
620
Chris Lattner9a023f72009-06-24 04:43:34 +0000621/// ParseDirectiveSection:
Chris Lattner529fb542009-06-24 05:13:15 +0000622/// ::= .section identifier (',' identifier)*
623/// FIXME: This should actually parse out the segment, section, attributes and
624/// sizeof_stub fields.
625bool AsmParser::ParseDirectiveDarwinSection() {
Daniel Dunbar3f872332009-07-28 16:08:33 +0000626 if (Lexer.isNot(AsmToken::Identifier))
Chris Lattner9a023f72009-06-24 04:43:34 +0000627 return TokError("expected identifier after '.section' directive");
628
Daniel Dunbar419aded2009-07-28 16:38:40 +0000629 std::string Section = Lexer.getTok().getString();
Chris Lattner9a023f72009-06-24 04:43:34 +0000630 Lexer.Lex();
631
632 // Accept a comma separated list of modifiers.
Daniel Dunbar3f872332009-07-28 16:08:33 +0000633 while (Lexer.is(AsmToken::Comma)) {
Chris Lattner9a023f72009-06-24 04:43:34 +0000634 Lexer.Lex();
635
Daniel Dunbar3f872332009-07-28 16:08:33 +0000636 if (Lexer.isNot(AsmToken::Identifier))
Chris Lattner9a023f72009-06-24 04:43:34 +0000637 return TokError("expected identifier in '.section' directive");
638 Section += ',';
Daniel Dunbar419aded2009-07-28 16:38:40 +0000639 Section += Lexer.getTok().getString().str();
Chris Lattner9a023f72009-06-24 04:43:34 +0000640 Lexer.Lex();
641 }
642
Daniel Dunbar3f872332009-07-28 16:08:33 +0000643 if (Lexer.isNot(AsmToken::EndOfStatement))
Chris Lattner9a023f72009-06-24 04:43:34 +0000644 return TokError("unexpected token in '.section' directive");
645 Lexer.Lex();
646
Chris Lattner56594f92009-07-31 17:47:16 +0000647 // FIXME: Arch specific.
648 MCSection *S = Ctx.GetSection(Section);
649 if (S == 0)
650 S = MCSection::Create(Section, Ctx);
651
652 Out.SwitchSection(S);
Chris Lattner9a023f72009-06-24 04:43:34 +0000653 return false;
654}
655
Chris Lattner529fb542009-06-24 05:13:15 +0000656bool AsmParser::ParseDirectiveSectionSwitch(const char *Section,
657 const char *Directives) {
Daniel Dunbar3f872332009-07-28 16:08:33 +0000658 if (Lexer.isNot(AsmToken::EndOfStatement))
Chris Lattner529fb542009-06-24 05:13:15 +0000659 return TokError("unexpected token in section switching directive");
660 Lexer.Lex();
661
662 std::string SectionStr = Section;
663 if (Directives && Directives[0]) {
664 SectionStr += ",";
665 SectionStr += Directives;
666 }
667
Chris Lattner56594f92009-07-31 17:47:16 +0000668 // FIXME: Arch specific.
669 MCSection *S = Ctx.GetSection(Section);
670 if (S == 0)
671 S = MCSection::Create(Section, Ctx);
672
673 Out.SwitchSection(S);
Chris Lattner529fb542009-06-24 05:13:15 +0000674 return false;
675}
Daniel Dunbara0d14262009-06-24 23:30:00 +0000676
677/// ParseDirectiveAscii:
Daniel Dunbar475839e2009-06-29 20:37:27 +0000678/// ::= ( .ascii | .asciz ) [ "string" ( , "string" )* ]
Daniel Dunbara0d14262009-06-24 23:30:00 +0000679bool AsmParser::ParseDirectiveAscii(bool ZeroTerminated) {
Daniel Dunbar3f872332009-07-28 16:08:33 +0000680 if (Lexer.isNot(AsmToken::EndOfStatement)) {
Daniel Dunbara0d14262009-06-24 23:30:00 +0000681 for (;;) {
Daniel Dunbar3f872332009-07-28 16:08:33 +0000682 if (Lexer.isNot(AsmToken::String))
Daniel Dunbara0d14262009-06-24 23:30:00 +0000683 return TokError("expected string in '.ascii' or '.asciz' directive");
684
685 // FIXME: This shouldn't use a const char* + strlen, the string could have
686 // embedded nulls.
687 // FIXME: Should have accessor for getting string contents.
Daniel Dunbar419aded2009-07-28 16:38:40 +0000688 StringRef Str = Lexer.getTok().getString();
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000689 Out.EmitBytes(Str.substr(1, Str.size() - 2));
Daniel Dunbara0d14262009-06-24 23:30:00 +0000690 if (ZeroTerminated)
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000691 Out.EmitBytes(StringRef("\0", 1));
Daniel Dunbara0d14262009-06-24 23:30:00 +0000692
693 Lexer.Lex();
694
Daniel Dunbar3f872332009-07-28 16:08:33 +0000695 if (Lexer.is(AsmToken::EndOfStatement))
Daniel Dunbara0d14262009-06-24 23:30:00 +0000696 break;
697
Daniel Dunbar3f872332009-07-28 16:08:33 +0000698 if (Lexer.isNot(AsmToken::Comma))
Daniel Dunbara0d14262009-06-24 23:30:00 +0000699 return TokError("unexpected token in '.ascii' or '.asciz' directive");
700 Lexer.Lex();
701 }
702 }
703
704 Lexer.Lex();
705 return false;
706}
707
708/// ParseDirectiveValue
709/// ::= (.byte | .short | ... ) [ expression (, expression)* ]
710bool AsmParser::ParseDirectiveValue(unsigned Size) {
Daniel Dunbar3f872332009-07-28 16:08:33 +0000711 if (Lexer.isNot(AsmToken::EndOfStatement)) {
Daniel Dunbara0d14262009-06-24 23:30:00 +0000712 for (;;) {
Daniel Dunbarf4b830f2009-06-30 02:10:03 +0000713 MCValue Expr;
714 if (ParseRelocatableExpression(Expr))
Daniel Dunbara0d14262009-06-24 23:30:00 +0000715 return true;
716
Daniel Dunbarf4b830f2009-06-30 02:10:03 +0000717 Out.EmitValue(Expr, Size);
Daniel Dunbara0d14262009-06-24 23:30:00 +0000718
Daniel Dunbar3f872332009-07-28 16:08:33 +0000719 if (Lexer.is(AsmToken::EndOfStatement))
Daniel Dunbara0d14262009-06-24 23:30:00 +0000720 break;
721
722 // FIXME: Improve diagnostic.
Daniel Dunbar3f872332009-07-28 16:08:33 +0000723 if (Lexer.isNot(AsmToken::Comma))
Daniel Dunbara0d14262009-06-24 23:30:00 +0000724 return TokError("unexpected token in directive");
725 Lexer.Lex();
726 }
727 }
728
729 Lexer.Lex();
730 return false;
731}
732
733/// ParseDirectiveSpace
734/// ::= .space expression [ , expression ]
735bool AsmParser::ParseDirectiveSpace() {
736 int64_t NumBytes;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000737 if (ParseAbsoluteExpression(NumBytes))
Daniel Dunbara0d14262009-06-24 23:30:00 +0000738 return true;
739
740 int64_t FillExpr = 0;
741 bool HasFillExpr = false;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000742 if (Lexer.isNot(AsmToken::EndOfStatement)) {
743 if (Lexer.isNot(AsmToken::Comma))
Daniel Dunbara0d14262009-06-24 23:30:00 +0000744 return TokError("unexpected token in '.space' directive");
745 Lexer.Lex();
746
Daniel Dunbar475839e2009-06-29 20:37:27 +0000747 if (ParseAbsoluteExpression(FillExpr))
Daniel Dunbara0d14262009-06-24 23:30:00 +0000748 return true;
749
750 HasFillExpr = true;
751
Daniel Dunbar3f872332009-07-28 16:08:33 +0000752 if (Lexer.isNot(AsmToken::EndOfStatement))
Daniel Dunbara0d14262009-06-24 23:30:00 +0000753 return TokError("unexpected token in '.space' directive");
754 }
755
756 Lexer.Lex();
757
758 if (NumBytes <= 0)
759 return TokError("invalid number of bytes in '.space' directive");
760
761 // FIXME: Sometimes the fill expr is 'nop' if it isn't supplied, instead of 0.
762 for (uint64_t i = 0, e = NumBytes; i != e; ++i)
763 Out.EmitValue(MCValue::get(FillExpr), 1);
764
765 return false;
766}
767
768/// ParseDirectiveFill
769/// ::= .fill expression , expression , expression
770bool AsmParser::ParseDirectiveFill() {
771 int64_t NumValues;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000772 if (ParseAbsoluteExpression(NumValues))
Daniel Dunbara0d14262009-06-24 23:30:00 +0000773 return true;
774
Daniel Dunbar3f872332009-07-28 16:08:33 +0000775 if (Lexer.isNot(AsmToken::Comma))
Daniel Dunbara0d14262009-06-24 23:30:00 +0000776 return TokError("unexpected token in '.fill' directive");
777 Lexer.Lex();
778
779 int64_t FillSize;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000780 if (ParseAbsoluteExpression(FillSize))
Daniel Dunbara0d14262009-06-24 23:30:00 +0000781 return true;
782
Daniel Dunbar3f872332009-07-28 16:08:33 +0000783 if (Lexer.isNot(AsmToken::Comma))
Daniel Dunbara0d14262009-06-24 23:30:00 +0000784 return TokError("unexpected token in '.fill' directive");
785 Lexer.Lex();
786
787 int64_t FillExpr;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000788 if (ParseAbsoluteExpression(FillExpr))
Daniel Dunbara0d14262009-06-24 23:30:00 +0000789 return true;
790
Daniel Dunbar3f872332009-07-28 16:08:33 +0000791 if (Lexer.isNot(AsmToken::EndOfStatement))
Daniel Dunbara0d14262009-06-24 23:30:00 +0000792 return TokError("unexpected token in '.fill' directive");
793
794 Lexer.Lex();
795
796 if (FillSize != 1 && FillSize != 2 && FillSize != 4)
797 return TokError("invalid '.fill' size, expected 1, 2, or 4");
798
799 for (uint64_t i = 0, e = NumValues; i != e; ++i)
800 Out.EmitValue(MCValue::get(FillExpr), FillSize);
801
802 return false;
803}
Daniel Dunbarc238b582009-06-25 22:44:51 +0000804
805/// ParseDirectiveOrg
806/// ::= .org expression [ , expression ]
807bool AsmParser::ParseDirectiveOrg() {
Daniel Dunbarf4b830f2009-06-30 02:10:03 +0000808 MCValue Offset;
809 if (ParseRelocatableExpression(Offset))
Daniel Dunbarc238b582009-06-25 22:44:51 +0000810 return true;
811
812 // Parse optional fill expression.
813 int64_t FillExpr = 0;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000814 if (Lexer.isNot(AsmToken::EndOfStatement)) {
815 if (Lexer.isNot(AsmToken::Comma))
Daniel Dunbarc238b582009-06-25 22:44:51 +0000816 return TokError("unexpected token in '.org' directive");
817 Lexer.Lex();
818
Daniel Dunbar475839e2009-06-29 20:37:27 +0000819 if (ParseAbsoluteExpression(FillExpr))
Daniel Dunbarc238b582009-06-25 22:44:51 +0000820 return true;
821
Daniel Dunbar3f872332009-07-28 16:08:33 +0000822 if (Lexer.isNot(AsmToken::EndOfStatement))
Daniel Dunbarc238b582009-06-25 22:44:51 +0000823 return TokError("unexpected token in '.org' directive");
824 }
825
826 Lexer.Lex();
Daniel Dunbarf4b830f2009-06-30 02:10:03 +0000827
828 // FIXME: Only limited forms of relocatable expressions are accepted here, it
829 // has to be relative to the current section.
830 Out.EmitValueToOffset(Offset, FillExpr);
Daniel Dunbarc238b582009-06-25 22:44:51 +0000831
832 return false;
833}
Daniel Dunbarc29dfa72009-06-29 23:46:59 +0000834
835/// ParseDirectiveAlign
836/// ::= {.align, ...} expression [ , expression [ , expression ]]
837bool AsmParser::ParseDirectiveAlign(bool IsPow2, unsigned ValueSize) {
838 int64_t Alignment;
839 if (ParseAbsoluteExpression(Alignment))
840 return true;
841
842 SMLoc MaxBytesLoc;
843 bool HasFillExpr = false;
844 int64_t FillExpr = 0;
845 int64_t MaxBytesToFill = 0;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000846 if (Lexer.isNot(AsmToken::EndOfStatement)) {
847 if (Lexer.isNot(AsmToken::Comma))
Daniel Dunbarc29dfa72009-06-29 23:46:59 +0000848 return TokError("unexpected token in directive");
849 Lexer.Lex();
850
851 // The fill expression can be omitted while specifying a maximum number of
852 // alignment bytes, e.g:
853 // .align 3,,4
Daniel Dunbar3f872332009-07-28 16:08:33 +0000854 if (Lexer.isNot(AsmToken::Comma)) {
Daniel Dunbarc29dfa72009-06-29 23:46:59 +0000855 HasFillExpr = true;
856 if (ParseAbsoluteExpression(FillExpr))
857 return true;
858 }
859
Daniel Dunbar3f872332009-07-28 16:08:33 +0000860 if (Lexer.isNot(AsmToken::EndOfStatement)) {
861 if (Lexer.isNot(AsmToken::Comma))
Daniel Dunbarc29dfa72009-06-29 23:46:59 +0000862 return TokError("unexpected token in directive");
863 Lexer.Lex();
864
865 MaxBytesLoc = Lexer.getLoc();
866 if (ParseAbsoluteExpression(MaxBytesToFill))
867 return true;
868
Daniel Dunbar3f872332009-07-28 16:08:33 +0000869 if (Lexer.isNot(AsmToken::EndOfStatement))
Daniel Dunbarc29dfa72009-06-29 23:46:59 +0000870 return TokError("unexpected token in directive");
871 }
872 }
873
874 Lexer.Lex();
875
876 if (!HasFillExpr) {
877 // FIXME: Sometimes fill with nop.
878 FillExpr = 0;
879 }
880
881 // Compute alignment in bytes.
882 if (IsPow2) {
883 // FIXME: Diagnose overflow.
Chris Lattner39750252009-07-11 22:32:37 +0000884 Alignment = 1LL << Alignment;
Daniel Dunbarc29dfa72009-06-29 23:46:59 +0000885 }
886
887 // Diagnose non-sensical max bytes to fill.
888 if (MaxBytesLoc.isValid()) {
889 if (MaxBytesToFill < 1) {
Daniel Dunbar3fb76832009-06-30 00:49:23 +0000890 Warning(MaxBytesLoc, "alignment directive can never be satisfied in this "
891 "many bytes, ignoring");
Daniel Dunbarc29dfa72009-06-29 23:46:59 +0000892 return false;
893 }
894
895 if (MaxBytesToFill >= Alignment) {
Daniel Dunbar3fb76832009-06-30 00:49:23 +0000896 Warning(MaxBytesLoc, "maximum bytes expression exceeds alignment and "
897 "has no effect");
Daniel Dunbarc29dfa72009-06-29 23:46:59 +0000898 MaxBytesToFill = 0;
899 }
900 }
901
902 // FIXME: Target specific behavior about how the "extra" bytes are filled.
903 Out.EmitValueToAlignment(Alignment, FillExpr, ValueSize, MaxBytesToFill);
904
905 return false;
906}
907
Daniel Dunbard7b267b2009-06-30 00:33:19 +0000908/// ParseDirectiveSymbolAttribute
909/// ::= { ".globl", ".weak", ... } [ identifier ( , identifier )* ]
910bool AsmParser::ParseDirectiveSymbolAttribute(MCStreamer::SymbolAttr Attr) {
Daniel Dunbar3f872332009-07-28 16:08:33 +0000911 if (Lexer.isNot(AsmToken::EndOfStatement)) {
Daniel Dunbard7b267b2009-06-30 00:33:19 +0000912 for (;;) {
Daniel Dunbar3f872332009-07-28 16:08:33 +0000913 if (Lexer.isNot(AsmToken::Identifier))
Daniel Dunbard7b267b2009-06-30 00:33:19 +0000914 return TokError("expected identifier in directive");
915
Daniel Dunbar419aded2009-07-28 16:38:40 +0000916 MCSymbol *Sym = Ctx.GetOrCreateSymbol(Lexer.getTok().getString());
Daniel Dunbard7b267b2009-06-30 00:33:19 +0000917 Lexer.Lex();
918
919 // If this is use of an undefined symbol then mark it external.
920 if (!Sym->getSection() && !Ctx.GetSymbolValue(Sym))
921 Sym->setExternal(true);
922
923 Out.EmitSymbolAttribute(Sym, Attr);
924
Daniel Dunbar3f872332009-07-28 16:08:33 +0000925 if (Lexer.is(AsmToken::EndOfStatement))
Daniel Dunbard7b267b2009-06-30 00:33:19 +0000926 break;
927
Daniel Dunbar3f872332009-07-28 16:08:33 +0000928 if (Lexer.isNot(AsmToken::Comma))
Daniel Dunbard7b267b2009-06-30 00:33:19 +0000929 return TokError("unexpected token in directive");
930 Lexer.Lex();
931 }
932 }
933
934 Lexer.Lex();
935 return false;
936}
Chris Lattner4e4db7a2009-07-07 20:30:46 +0000937
Kevin Enderby95cf30c2009-07-14 18:17:10 +0000938/// ParseDirectiveDarwinSymbolDesc
939/// ::= .desc identifier , expression
940bool AsmParser::ParseDirectiveDarwinSymbolDesc() {
Daniel Dunbar3f872332009-07-28 16:08:33 +0000941 if (Lexer.isNot(AsmToken::Identifier))
Kevin Enderby95cf30c2009-07-14 18:17:10 +0000942 return TokError("expected identifier in directive");
943
944 // handle the identifier as the key symbol.
945 SMLoc IDLoc = Lexer.getLoc();
Daniel Dunbar419aded2009-07-28 16:38:40 +0000946 MCSymbol *Sym = Ctx.GetOrCreateSymbol(Lexer.getTok().getString());
Kevin Enderby95cf30c2009-07-14 18:17:10 +0000947 Lexer.Lex();
948
Daniel Dunbar3f872332009-07-28 16:08:33 +0000949 if (Lexer.isNot(AsmToken::Comma))
Kevin Enderby95cf30c2009-07-14 18:17:10 +0000950 return TokError("unexpected token in '.desc' directive");
951 Lexer.Lex();
952
953 SMLoc DescLoc = Lexer.getLoc();
954 int64_t DescValue;
955 if (ParseAbsoluteExpression(DescValue))
956 return true;
957
Daniel Dunbar3f872332009-07-28 16:08:33 +0000958 if (Lexer.isNot(AsmToken::EndOfStatement))
Kevin Enderby95cf30c2009-07-14 18:17:10 +0000959 return TokError("unexpected token in '.desc' directive");
960
961 Lexer.Lex();
962
963 // Set the n_desc field of this Symbol to this DescValue
964 Out.EmitSymbolDesc(Sym, DescValue);
965
966 return false;
967}
968
Chris Lattner4e4db7a2009-07-07 20:30:46 +0000969/// ParseDirectiveComm
Chris Lattner1fc3d752009-07-09 17:25:12 +0000970/// ::= ( .comm | .lcomm ) identifier , size_expression [ , align_expression ]
971bool AsmParser::ParseDirectiveComm(bool IsLocal) {
Daniel Dunbar3f872332009-07-28 16:08:33 +0000972 if (Lexer.isNot(AsmToken::Identifier))
Chris Lattner4e4db7a2009-07-07 20:30:46 +0000973 return TokError("expected identifier in directive");
974
Daniel Dunbar76c4d762009-07-31 21:55:09 +0000975 // Handle the identifier as the key symbol.
Chris Lattner4e4db7a2009-07-07 20:30:46 +0000976 SMLoc IDLoc = Lexer.getLoc();
Daniel Dunbar419aded2009-07-28 16:38:40 +0000977 MCSymbol *Sym = Ctx.GetOrCreateSymbol(Lexer.getTok().getString());
Chris Lattner4e4db7a2009-07-07 20:30:46 +0000978 Lexer.Lex();
979
Daniel Dunbar3f872332009-07-28 16:08:33 +0000980 if (Lexer.isNot(AsmToken::Comma))
Chris Lattner4e4db7a2009-07-07 20:30:46 +0000981 return TokError("unexpected token in directive");
982 Lexer.Lex();
983
984 int64_t Size;
985 SMLoc SizeLoc = Lexer.getLoc();
986 if (ParseAbsoluteExpression(Size))
987 return true;
988
989 int64_t Pow2Alignment = 0;
990 SMLoc Pow2AlignmentLoc;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000991 if (Lexer.is(AsmToken::Comma)) {
Chris Lattner4e4db7a2009-07-07 20:30:46 +0000992 Lexer.Lex();
993 Pow2AlignmentLoc = Lexer.getLoc();
994 if (ParseAbsoluteExpression(Pow2Alignment))
995 return true;
996 }
997
Daniel Dunbar3f872332009-07-28 16:08:33 +0000998 if (Lexer.isNot(AsmToken::EndOfStatement))
Chris Lattner1fc3d752009-07-09 17:25:12 +0000999 return TokError("unexpected token in '.comm' or '.lcomm' directive");
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001000
1001 Lexer.Lex();
1002
Chris Lattner1fc3d752009-07-09 17:25:12 +00001003 // NOTE: a size of zero for a .comm should create a undefined symbol
1004 // but a size of .lcomm creates a bss symbol of size zero.
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001005 if (Size < 0)
Chris Lattner1fc3d752009-07-09 17:25:12 +00001006 return Error(SizeLoc, "invalid '.comm' or '.lcomm' directive size, can't "
1007 "be less than zero");
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001008
1009 // NOTE: The alignment in the directive is a power of 2 value, the assember
1010 // may internally end up wanting an alignment in bytes.
1011 // FIXME: Diagnose overflow.
1012 if (Pow2Alignment < 0)
Chris Lattner1fc3d752009-07-09 17:25:12 +00001013 return Error(Pow2AlignmentLoc, "invalid '.comm' or '.lcomm' directive "
1014 "alignment, can't be less than zero");
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001015
1016 // TODO: Symbol must be undefined or it is a error to re-defined the symbol
1017 if (Sym->getSection() || Ctx.GetSymbolValue(Sym))
1018 return Error(IDLoc, "invalid symbol redefinition");
1019
Chris Lattner1fc3d752009-07-09 17:25:12 +00001020 // Create the Symbol as a common or local common with Size and Pow2Alignment
1021 Out.EmitCommonSymbol(Sym, Size, Pow2Alignment, IsLocal);
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001022
1023 return false;
1024}
Chris Lattner9be3fee2009-07-10 22:20:30 +00001025
1026/// ParseDirectiveDarwinZerofill
1027/// ::= .zerofill segname , sectname [, identifier , size_expression [
1028/// , align_expression ]]
1029bool AsmParser::ParseDirectiveDarwinZerofill() {
Daniel Dunbar3f872332009-07-28 16:08:33 +00001030 if (Lexer.isNot(AsmToken::Identifier))
Chris Lattner9be3fee2009-07-10 22:20:30 +00001031 return TokError("expected segment name after '.zerofill' directive");
Daniel Dunbar419aded2009-07-28 16:38:40 +00001032 std::string Section = Lexer.getTok().getString();
Chris Lattner9be3fee2009-07-10 22:20:30 +00001033 Lexer.Lex();
1034
Daniel Dunbar3f872332009-07-28 16:08:33 +00001035 if (Lexer.isNot(AsmToken::Comma))
Chris Lattner9be3fee2009-07-10 22:20:30 +00001036 return TokError("unexpected token in directive");
1037 Section += ',';
1038 Lexer.Lex();
1039
Daniel Dunbar3f872332009-07-28 16:08:33 +00001040 if (Lexer.isNot(AsmToken::Identifier))
Chris Lattner9be3fee2009-07-10 22:20:30 +00001041 return TokError("expected section name after comma in '.zerofill' "
1042 "directive");
Daniel Dunbar419aded2009-07-28 16:38:40 +00001043 Section += Lexer.getTok().getString().str();
Chris Lattner9be3fee2009-07-10 22:20:30 +00001044 Lexer.Lex();
1045
1046 // FIXME: we will need to tell GetSection() that this is to be created with or
1047 // must have the Mach-O section type of S_ZEROFILL. Something like the code
1048 // below could be done but for now it is not as EmitZerofill() does not know
1049 // how to deal with a section type in the section name like
1050 // ParseDirectiveDarwinSection() allows.
1051 // Section += ',';
1052 // Section += "zerofill";
1053
1054 // If this is the end of the line all that was wanted was to create the
1055 // the section but with no symbol.
Daniel Dunbar3f872332009-07-28 16:08:33 +00001056 if (Lexer.is(AsmToken::EndOfStatement)) {
Chris Lattner6bdd74c2009-07-31 18:27:48 +00001057 // FIXME: Arch specific.
1058 MCSection *S = Ctx.GetSection(Section);
1059 if (S == 0)
1060 S = MCSection::Create(Section, Ctx);
1061
Chris Lattner9be3fee2009-07-10 22:20:30 +00001062 // Create the zerofill section but no symbol
Chris Lattner6bdd74c2009-07-31 18:27:48 +00001063 Out.EmitZerofill(S);
Chris Lattner9be3fee2009-07-10 22:20:30 +00001064 return false;
1065 }
1066
Daniel Dunbar3f872332009-07-28 16:08:33 +00001067 if (Lexer.isNot(AsmToken::Comma))
Chris Lattner9be3fee2009-07-10 22:20:30 +00001068 return TokError("unexpected token in directive");
1069 Lexer.Lex();
1070
Daniel Dunbar3f872332009-07-28 16:08:33 +00001071 if (Lexer.isNot(AsmToken::Identifier))
Chris Lattner9be3fee2009-07-10 22:20:30 +00001072 return TokError("expected identifier in directive");
1073
1074 // handle the identifier as the key symbol.
1075 SMLoc IDLoc = Lexer.getLoc();
Daniel Dunbar419aded2009-07-28 16:38:40 +00001076 MCSymbol *Sym = Ctx.GetOrCreateSymbol(Lexer.getTok().getString());
Chris Lattner9be3fee2009-07-10 22:20:30 +00001077 Lexer.Lex();
1078
Daniel Dunbar3f872332009-07-28 16:08:33 +00001079 if (Lexer.isNot(AsmToken::Comma))
Chris Lattner9be3fee2009-07-10 22:20:30 +00001080 return TokError("unexpected token in directive");
1081 Lexer.Lex();
1082
1083 int64_t Size;
1084 SMLoc SizeLoc = Lexer.getLoc();
1085 if (ParseAbsoluteExpression(Size))
1086 return true;
1087
1088 int64_t Pow2Alignment = 0;
1089 SMLoc Pow2AlignmentLoc;
Daniel Dunbar3f872332009-07-28 16:08:33 +00001090 if (Lexer.is(AsmToken::Comma)) {
Chris Lattner9be3fee2009-07-10 22:20:30 +00001091 Lexer.Lex();
1092 Pow2AlignmentLoc = Lexer.getLoc();
1093 if (ParseAbsoluteExpression(Pow2Alignment))
1094 return true;
1095 }
1096
Daniel Dunbar3f872332009-07-28 16:08:33 +00001097 if (Lexer.isNot(AsmToken::EndOfStatement))
Chris Lattner9be3fee2009-07-10 22:20:30 +00001098 return TokError("unexpected token in '.zerofill' directive");
1099
1100 Lexer.Lex();
1101
1102 if (Size < 0)
1103 return Error(SizeLoc, "invalid '.zerofill' directive size, can't be less "
1104 "than zero");
1105
1106 // NOTE: The alignment in the directive is a power of 2 value, the assember
1107 // may internally end up wanting an alignment in bytes.
1108 // FIXME: Diagnose overflow.
1109 if (Pow2Alignment < 0)
1110 return Error(Pow2AlignmentLoc, "invalid '.zerofill' directive alignment, "
1111 "can't be less than zero");
1112
1113 // TODO: Symbol must be undefined or it is a error to re-defined the symbol
1114 if (Sym->getSection() || Ctx.GetSymbolValue(Sym))
1115 return Error(IDLoc, "invalid symbol redefinition");
1116
Chris Lattner6bdd74c2009-07-31 18:27:48 +00001117 // FIXME: Arch specific.
1118 MCSection *S = Ctx.GetSection(Section);
1119 if (S == 0)
1120 S = MCSection::Create(Section, Ctx);
1121
Chris Lattner9be3fee2009-07-10 22:20:30 +00001122 // Create the zerofill Symbol with Size and Pow2Alignment
Chris Lattner6bdd74c2009-07-31 18:27:48 +00001123 Out.EmitZerofill(S, Sym, Size, Pow2Alignment);
Chris Lattner9be3fee2009-07-10 22:20:30 +00001124
1125 return false;
1126}
Kevin Enderbya5c78322009-07-13 21:03:15 +00001127
1128/// ParseDirectiveDarwinSubsectionsViaSymbols
1129/// ::= .subsections_via_symbols
1130bool AsmParser::ParseDirectiveDarwinSubsectionsViaSymbols() {
Daniel Dunbar3f872332009-07-28 16:08:33 +00001131 if (Lexer.isNot(AsmToken::EndOfStatement))
Kevin Enderbya5c78322009-07-13 21:03:15 +00001132 return TokError("unexpected token in '.subsections_via_symbols' directive");
1133
1134 Lexer.Lex();
1135
Kevin Enderbyf96db462009-07-16 17:56:39 +00001136 Out.EmitAssemblerFlag(MCStreamer::SubsectionsViaSymbols);
Kevin Enderbya5c78322009-07-13 21:03:15 +00001137
1138 return false;
1139}
Kevin Enderby5f1f0b82009-07-13 23:15:14 +00001140
1141/// ParseDirectiveAbort
1142/// ::= .abort [ "abort_string" ]
1143bool AsmParser::ParseDirectiveAbort() {
Daniel Dunbarf9507ff2009-07-27 23:20:52 +00001144 // FIXME: Use loc from directive.
1145 SMLoc Loc = Lexer.getLoc();
1146
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001147 StringRef Str = "";
Daniel Dunbar3f872332009-07-28 16:08:33 +00001148 if (Lexer.isNot(AsmToken::EndOfStatement)) {
1149 if (Lexer.isNot(AsmToken::String))
Kevin Enderby5f1f0b82009-07-13 23:15:14 +00001150 return TokError("expected string in '.abort' directive");
1151
Daniel Dunbar419aded2009-07-28 16:38:40 +00001152 Str = Lexer.getTok().getString();
Kevin Enderby5f1f0b82009-07-13 23:15:14 +00001153
1154 Lexer.Lex();
1155 }
1156
Daniel Dunbar3f872332009-07-28 16:08:33 +00001157 if (Lexer.isNot(AsmToken::EndOfStatement))
Kevin Enderby5f1f0b82009-07-13 23:15:14 +00001158 return TokError("unexpected token in '.abort' directive");
1159
1160 Lexer.Lex();
1161
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001162 // FIXME: Handle here.
Daniel Dunbarf9507ff2009-07-27 23:20:52 +00001163 if (Str.empty())
1164 Error(Loc, ".abort detected. Assembly stopping.");
1165 else
1166 Error(Loc, ".abort '" + Str + "' detected. Assembly stopping.");
Kevin Enderby5f1f0b82009-07-13 23:15:14 +00001167
1168 return false;
1169}
Kevin Enderby71148242009-07-14 21:35:03 +00001170
1171/// ParseDirectiveLsym
1172/// ::= .lsym identifier , expression
1173bool AsmParser::ParseDirectiveDarwinLsym() {
Daniel Dunbar3f872332009-07-28 16:08:33 +00001174 if (Lexer.isNot(AsmToken::Identifier))
Kevin Enderby71148242009-07-14 21:35:03 +00001175 return TokError("expected identifier in directive");
1176
Daniel Dunbar76c4d762009-07-31 21:55:09 +00001177 // Handle the identifier as the key symbol.
Kevin Enderby71148242009-07-14 21:35:03 +00001178 SMLoc IDLoc = Lexer.getLoc();
Daniel Dunbar419aded2009-07-28 16:38:40 +00001179 MCSymbol *Sym = Ctx.GetOrCreateSymbol(Lexer.getTok().getString());
Kevin Enderby71148242009-07-14 21:35:03 +00001180 Lexer.Lex();
1181
Daniel Dunbar3f872332009-07-28 16:08:33 +00001182 if (Lexer.isNot(AsmToken::Comma))
Kevin Enderby71148242009-07-14 21:35:03 +00001183 return TokError("unexpected token in '.lsym' directive");
1184 Lexer.Lex();
1185
1186 MCValue Expr;
1187 if (ParseRelocatableExpression(Expr))
1188 return true;
1189
Daniel Dunbar3f872332009-07-28 16:08:33 +00001190 if (Lexer.isNot(AsmToken::EndOfStatement))
Kevin Enderby71148242009-07-14 21:35:03 +00001191 return TokError("unexpected token in '.lsym' directive");
1192
1193 Lexer.Lex();
1194
1195 // Create the Sym with the value of the Expr
1196 Out.EmitLocalSymbol(Sym, Expr);
1197
1198 return false;
1199}
Kevin Enderby1f049b22009-07-14 23:21:55 +00001200
1201/// ParseDirectiveInclude
1202/// ::= .include "filename"
1203bool AsmParser::ParseDirectiveInclude() {
Daniel Dunbar3f872332009-07-28 16:08:33 +00001204 if (Lexer.isNot(AsmToken::String))
Kevin Enderby1f049b22009-07-14 23:21:55 +00001205 return TokError("expected string in '.include' directive");
1206
Daniel Dunbar419aded2009-07-28 16:38:40 +00001207 std::string Filename = Lexer.getTok().getString();
Chris Lattner8e25e2d2009-07-16 06:14:39 +00001208 SMLoc IncludeLoc = Lexer.getLoc();
Kevin Enderby1f049b22009-07-14 23:21:55 +00001209 Lexer.Lex();
1210
Daniel Dunbar3f872332009-07-28 16:08:33 +00001211 if (Lexer.isNot(AsmToken::EndOfStatement))
Kevin Enderby1f049b22009-07-14 23:21:55 +00001212 return TokError("unexpected token in '.include' directive");
1213
Chris Lattner8e25e2d2009-07-16 06:14:39 +00001214 // Strip the quotes.
1215 Filename = Filename.substr(1, Filename.size()-2);
1216
1217 // Attempt to switch the lexer to the included file before consuming the end
1218 // of statement to avoid losing it when we switch.
1219 if (Lexer.EnterIncludeFile(Filename)) {
1220 Lexer.PrintMessage(IncludeLoc,
1221 "Could not find include file '" + Filename + "'",
1222 "error");
1223 return true;
1224 }
Kevin Enderby1f049b22009-07-14 23:21:55 +00001225
1226 return false;
1227}
Kevin Enderby6e68cd92009-07-15 15:30:11 +00001228
1229/// ParseDirectiveDarwinDumpOrLoad
1230/// ::= ( .dump | .load ) "filename"
Kevin Enderby5026ae42009-07-20 20:25:37 +00001231bool AsmParser::ParseDirectiveDarwinDumpOrLoad(SMLoc IDLoc, bool IsDump) {
Daniel Dunbar3f872332009-07-28 16:08:33 +00001232 if (Lexer.isNot(AsmToken::String))
Kevin Enderby6e68cd92009-07-15 15:30:11 +00001233 return TokError("expected string in '.dump' or '.load' directive");
1234
Kevin Enderby6e68cd92009-07-15 15:30:11 +00001235 Lexer.Lex();
1236
Daniel Dunbar3f872332009-07-28 16:08:33 +00001237 if (Lexer.isNot(AsmToken::EndOfStatement))
Kevin Enderby6e68cd92009-07-15 15:30:11 +00001238 return TokError("unexpected token in '.dump' or '.load' directive");
1239
1240 Lexer.Lex();
1241
Kevin Enderby5026ae42009-07-20 20:25:37 +00001242 // FIXME: If/when .dump and .load are implemented they will be done in the
1243 // the assembly parser and not have any need for an MCStreamer API.
Kevin Enderby6e68cd92009-07-15 15:30:11 +00001244 if (IsDump)
Kevin Enderby5026ae42009-07-20 20:25:37 +00001245 Warning(IDLoc, "ignoring directive .dump for now");
Kevin Enderby6e68cd92009-07-15 15:30:11 +00001246 else
Kevin Enderby5026ae42009-07-20 20:25:37 +00001247 Warning(IDLoc, "ignoring directive .load for now");
Kevin Enderby6e68cd92009-07-15 15:30:11 +00001248
1249 return false;
1250}