blob: 5bb1ae44df37faa2347077ba96e393dc263cd0a5 [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() {
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +0000306 if (Lexer.is(AsmToken::EndOfStatement)) {
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000307 Lexer.Lex();
308 return false;
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000309 }
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +0000310
311 // Statements always start with an identifier.
Daniel Dunbar419aded2009-07-28 16:38:40 +0000312 AsmToken ID = Lexer.getTok();
313 SMLoc IDLoc = ID.getLoc();
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +0000314 StringRef IDVal;
315 if (ParseIdentifier(IDVal))
316 return TokError("unexpected token at start of statement");
317
318 // FIXME: Recurse on local labels?
319
320 // See what kind of statement we have.
321 switch (Lexer.getKind()) {
Daniel Dunbar3f872332009-07-28 16:08:33 +0000322 case AsmToken::Colon: {
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000323 // identifier ':' -> Label.
324 Lexer.Lex();
Daniel Dunbardce0f3c2009-06-29 23:43:14 +0000325
326 // Diagnose attempt to use a variable as a label.
327 //
328 // FIXME: Diagnostics. Note the location of the definition as a label.
329 // FIXME: This doesn't diagnose assignment to a symbol which has been
330 // implicitly marked as external.
331 MCSymbol *Sym = Ctx.GetOrCreateSymbol(IDVal);
332 if (Sym->getSection())
333 return Error(IDLoc, "invalid symbol redefinition");
334 if (Ctx.GetSymbolValue(Sym))
335 return Error(IDLoc, "symbol already used as assembler variable");
Chris Lattnerc69485e2009-06-24 04:31:49 +0000336
337 // Since we saw a label, create a symbol and emit it.
338 // FIXME: If the label starts with L it is an assembler temporary label.
339 // Why does the client of this api need to know this?
Daniel Dunbardce0f3c2009-06-29 23:43:14 +0000340 Out.EmitLabel(Sym);
341
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000342 return ParseStatement();
Daniel Dunbardce0f3c2009-06-29 23:43:14 +0000343 }
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000344
Daniel Dunbar3f872332009-07-28 16:08:33 +0000345 case AsmToken::Equal:
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000346 // identifier '=' ... -> assignment statement
347 Lexer.Lex();
348
349 return ParseAssignment(IDVal, false);
350
351 default: // Normal instruction or directive.
352 break;
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000353 }
354
355 // Otherwise, we have a normal instruction or directive.
Chris Lattner2cf5f142009-06-22 01:29:09 +0000356 if (IDVal[0] == '.') {
Chris Lattner529fb542009-06-24 05:13:15 +0000357 // FIXME: This should be driven based on a hash lookup and callback.
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000358 if (IDVal == ".section")
Chris Lattner529fb542009-06-24 05:13:15 +0000359 return ParseDirectiveDarwinSection();
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000360 if (IDVal == ".text")
Chris Lattner529fb542009-06-24 05:13:15 +0000361 // FIXME: This changes behavior based on the -static flag to the
362 // assembler.
363 return ParseDirectiveSectionSwitch("__TEXT,__text",
364 "regular,pure_instructions");
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000365 if (IDVal == ".const")
Chris Lattner529fb542009-06-24 05:13:15 +0000366 return ParseDirectiveSectionSwitch("__TEXT,__const");
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000367 if (IDVal == ".static_const")
Chris Lattner529fb542009-06-24 05:13:15 +0000368 return ParseDirectiveSectionSwitch("__TEXT,__static_const");
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000369 if (IDVal == ".cstring")
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000370 return ParseDirectiveSectionSwitch("__TEXT,__cstring",
371 "cstring_literals");
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000372 if (IDVal == ".literal4")
Chris Lattner529fb542009-06-24 05:13:15 +0000373 return ParseDirectiveSectionSwitch("__TEXT,__literal4", "4byte_literals");
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000374 if (IDVal == ".literal8")
Chris Lattner529fb542009-06-24 05:13:15 +0000375 return ParseDirectiveSectionSwitch("__TEXT,__literal8", "8byte_literals");
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000376 if (IDVal == ".literal16")
Chris Lattner529fb542009-06-24 05:13:15 +0000377 return ParseDirectiveSectionSwitch("__TEXT,__literal16",
378 "16byte_literals");
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000379 if (IDVal == ".constructor")
Chris Lattner529fb542009-06-24 05:13:15 +0000380 return ParseDirectiveSectionSwitch("__TEXT,__constructor");
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000381 if (IDVal == ".destructor")
Chris Lattner529fb542009-06-24 05:13:15 +0000382 return ParseDirectiveSectionSwitch("__TEXT,__destructor");
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000383 if (IDVal == ".fvmlib_init0")
Chris Lattner529fb542009-06-24 05:13:15 +0000384 return ParseDirectiveSectionSwitch("__TEXT,__fvmlib_init0");
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000385 if (IDVal == ".fvmlib_init1")
Chris Lattner529fb542009-06-24 05:13:15 +0000386 return ParseDirectiveSectionSwitch("__TEXT,__fvmlib_init1");
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000387 if (IDVal == ".symbol_stub") // FIXME: Different on PPC.
Chris Lattner529fb542009-06-24 05:13:15 +0000388 return ParseDirectiveSectionSwitch("__IMPORT,__jump_table,symbol_stubs",
389 "self_modifying_code+pure_instructions,5");
390 // FIXME: .picsymbol_stub on PPC.
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000391 if (IDVal == ".data")
Chris Lattner529fb542009-06-24 05:13:15 +0000392 return ParseDirectiveSectionSwitch("__DATA,__data");
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000393 if (IDVal == ".static_data")
Chris Lattner529fb542009-06-24 05:13:15 +0000394 return ParseDirectiveSectionSwitch("__DATA,__static_data");
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000395 if (IDVal == ".non_lazy_symbol_pointer")
Chris Lattner529fb542009-06-24 05:13:15 +0000396 return ParseDirectiveSectionSwitch("__DATA,__nl_symbol_pointer",
397 "non_lazy_symbol_pointers");
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000398 if (IDVal == ".lazy_symbol_pointer")
Chris Lattner529fb542009-06-24 05:13:15 +0000399 return ParseDirectiveSectionSwitch("__DATA,__la_symbol_pointer",
400 "lazy_symbol_pointers");
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000401 if (IDVal == ".dyld")
Chris Lattner529fb542009-06-24 05:13:15 +0000402 return ParseDirectiveSectionSwitch("__DATA,__dyld");
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000403 if (IDVal == ".mod_init_func")
Chris Lattner529fb542009-06-24 05:13:15 +0000404 return ParseDirectiveSectionSwitch("__DATA,__mod_init_func",
405 "mod_init_funcs");
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000406 if (IDVal == ".mod_term_func")
Chris Lattner529fb542009-06-24 05:13:15 +0000407 return ParseDirectiveSectionSwitch("__DATA,__mod_term_func",
408 "mod_term_funcs");
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000409 if (IDVal == ".const_data")
Chris Lattner529fb542009-06-24 05:13:15 +0000410 return ParseDirectiveSectionSwitch("__DATA,__const", "regular");
411
412
413 // FIXME: Verify attributes on sections.
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000414 if (IDVal == ".objc_class")
Chris Lattner529fb542009-06-24 05:13:15 +0000415 return ParseDirectiveSectionSwitch("__OBJC,__class");
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000416 if (IDVal == ".objc_meta_class")
Chris Lattner529fb542009-06-24 05:13:15 +0000417 return ParseDirectiveSectionSwitch("__OBJC,__meta_class");
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000418 if (IDVal == ".objc_cat_cls_meth")
Chris Lattner529fb542009-06-24 05:13:15 +0000419 return ParseDirectiveSectionSwitch("__OBJC,__cat_cls_meth");
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000420 if (IDVal == ".objc_cat_inst_meth")
Chris Lattner529fb542009-06-24 05:13:15 +0000421 return ParseDirectiveSectionSwitch("__OBJC,__cat_inst_meth");
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000422 if (IDVal == ".objc_protocol")
Chris Lattner529fb542009-06-24 05:13:15 +0000423 return ParseDirectiveSectionSwitch("__OBJC,__protocol");
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000424 if (IDVal == ".objc_string_object")
Chris Lattner529fb542009-06-24 05:13:15 +0000425 return ParseDirectiveSectionSwitch("__OBJC,__string_object");
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000426 if (IDVal == ".objc_cls_meth")
Chris Lattner529fb542009-06-24 05:13:15 +0000427 return ParseDirectiveSectionSwitch("__OBJC,__cls_meth");
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000428 if (IDVal == ".objc_inst_meth")
Chris Lattner529fb542009-06-24 05:13:15 +0000429 return ParseDirectiveSectionSwitch("__OBJC,__inst_meth");
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000430 if (IDVal == ".objc_cls_refs")
Chris Lattner529fb542009-06-24 05:13:15 +0000431 return ParseDirectiveSectionSwitch("__OBJC,__cls_refs");
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000432 if (IDVal == ".objc_message_refs")
Chris Lattner529fb542009-06-24 05:13:15 +0000433 return ParseDirectiveSectionSwitch("__OBJC,__message_refs");
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000434 if (IDVal == ".objc_symbols")
Chris Lattner529fb542009-06-24 05:13:15 +0000435 return ParseDirectiveSectionSwitch("__OBJC,__symbols");
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000436 if (IDVal == ".objc_category")
Chris Lattner529fb542009-06-24 05:13:15 +0000437 return ParseDirectiveSectionSwitch("__OBJC,__category");
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000438 if (IDVal == ".objc_class_vars")
Chris Lattner529fb542009-06-24 05:13:15 +0000439 return ParseDirectiveSectionSwitch("__OBJC,__class_vars");
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000440 if (IDVal == ".objc_instance_vars")
Chris Lattner529fb542009-06-24 05:13:15 +0000441 return ParseDirectiveSectionSwitch("__OBJC,__instance_vars");
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000442 if (IDVal == ".objc_module_info")
Chris Lattner529fb542009-06-24 05:13:15 +0000443 return ParseDirectiveSectionSwitch("__OBJC,__module_info");
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000444 if (IDVal == ".objc_class_names")
Chris Lattner529fb542009-06-24 05:13:15 +0000445 return ParseDirectiveSectionSwitch("__TEXT,__cstring","cstring_literals");
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000446 if (IDVal == ".objc_meth_var_types")
Chris Lattner529fb542009-06-24 05:13:15 +0000447 return ParseDirectiveSectionSwitch("__TEXT,__cstring","cstring_literals");
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000448 if (IDVal == ".objc_meth_var_names")
Chris Lattner529fb542009-06-24 05:13:15 +0000449 return ParseDirectiveSectionSwitch("__TEXT,__cstring","cstring_literals");
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000450 if (IDVal == ".objc_selector_strs")
Chris Lattner529fb542009-06-24 05:13:15 +0000451 return ParseDirectiveSectionSwitch("__OBJC,__selector_strs");
Chris Lattner9a023f72009-06-24 04:43:34 +0000452
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000453 // Assembler features
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000454 if (IDVal == ".set")
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000455 return ParseDirectiveSet();
456
Daniel Dunbara0d14262009-06-24 23:30:00 +0000457 // Data directives
458
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000459 if (IDVal == ".ascii")
Daniel Dunbara0d14262009-06-24 23:30:00 +0000460 return ParseDirectiveAscii(false);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000461 if (IDVal == ".asciz")
Daniel Dunbara0d14262009-06-24 23:30:00 +0000462 return ParseDirectiveAscii(true);
463
464 // FIXME: Target hooks for size? Also for "word", "hword".
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000465 if (IDVal == ".byte")
Daniel Dunbara0d14262009-06-24 23:30:00 +0000466 return ParseDirectiveValue(1);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000467 if (IDVal == ".short")
Daniel Dunbara0d14262009-06-24 23:30:00 +0000468 return ParseDirectiveValue(2);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000469 if (IDVal == ".long")
Daniel Dunbara0d14262009-06-24 23:30:00 +0000470 return ParseDirectiveValue(4);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000471 if (IDVal == ".quad")
Daniel Dunbara0d14262009-06-24 23:30:00 +0000472 return ParseDirectiveValue(8);
Daniel Dunbarc29dfa72009-06-29 23:46:59 +0000473
474 // FIXME: Target hooks for IsPow2.
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000475 if (IDVal == ".align")
Daniel Dunbarc29dfa72009-06-29 23:46:59 +0000476 return ParseDirectiveAlign(/*IsPow2=*/true, /*ExprSize=*/1);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000477 if (IDVal == ".align32")
Daniel Dunbarc29dfa72009-06-29 23:46:59 +0000478 return ParseDirectiveAlign(/*IsPow2=*/true, /*ExprSize=*/4);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000479 if (IDVal == ".balign")
Daniel Dunbarc29dfa72009-06-29 23:46:59 +0000480 return ParseDirectiveAlign(/*IsPow2=*/false, /*ExprSize=*/1);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000481 if (IDVal == ".balignw")
Daniel Dunbarc29dfa72009-06-29 23:46:59 +0000482 return ParseDirectiveAlign(/*IsPow2=*/false, /*ExprSize=*/2);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000483 if (IDVal == ".balignl")
Daniel Dunbarc29dfa72009-06-29 23:46:59 +0000484 return ParseDirectiveAlign(/*IsPow2=*/false, /*ExprSize=*/4);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000485 if (IDVal == ".p2align")
Daniel Dunbarc29dfa72009-06-29 23:46:59 +0000486 return ParseDirectiveAlign(/*IsPow2=*/true, /*ExprSize=*/1);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000487 if (IDVal == ".p2alignw")
Daniel Dunbarc29dfa72009-06-29 23:46:59 +0000488 return ParseDirectiveAlign(/*IsPow2=*/true, /*ExprSize=*/2);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000489 if (IDVal == ".p2alignl")
Daniel Dunbarc29dfa72009-06-29 23:46:59 +0000490 return ParseDirectiveAlign(/*IsPow2=*/true, /*ExprSize=*/4);
491
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000492 if (IDVal == ".org")
Daniel Dunbarc238b582009-06-25 22:44:51 +0000493 return ParseDirectiveOrg();
Daniel Dunbarc29dfa72009-06-29 23:46:59 +0000494
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000495 if (IDVal == ".fill")
Daniel Dunbarc29dfa72009-06-29 23:46:59 +0000496 return ParseDirectiveFill();
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000497 if (IDVal == ".space")
Daniel Dunbara0d14262009-06-24 23:30:00 +0000498 return ParseDirectiveSpace();
499
Daniel Dunbard7b267b2009-06-30 00:33:19 +0000500 // Symbol attribute directives
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000501 if (IDVal == ".globl" || IDVal == ".global")
Daniel Dunbard7b267b2009-06-30 00:33:19 +0000502 return ParseDirectiveSymbolAttribute(MCStreamer::Global);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000503 if (IDVal == ".hidden")
Daniel Dunbard7b267b2009-06-30 00:33:19 +0000504 return ParseDirectiveSymbolAttribute(MCStreamer::Hidden);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000505 if (IDVal == ".indirect_symbol")
Daniel Dunbard7b267b2009-06-30 00:33:19 +0000506 return ParseDirectiveSymbolAttribute(MCStreamer::IndirectSymbol);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000507 if (IDVal == ".internal")
Daniel Dunbard7b267b2009-06-30 00:33:19 +0000508 return ParseDirectiveSymbolAttribute(MCStreamer::Internal);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000509 if (IDVal == ".lazy_reference")
Daniel Dunbard7b267b2009-06-30 00:33:19 +0000510 return ParseDirectiveSymbolAttribute(MCStreamer::LazyReference);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000511 if (IDVal == ".no_dead_strip")
Daniel Dunbard7b267b2009-06-30 00:33:19 +0000512 return ParseDirectiveSymbolAttribute(MCStreamer::NoDeadStrip);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000513 if (IDVal == ".private_extern")
Daniel Dunbard7b267b2009-06-30 00:33:19 +0000514 return ParseDirectiveSymbolAttribute(MCStreamer::PrivateExtern);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000515 if (IDVal == ".protected")
Daniel Dunbard7b267b2009-06-30 00:33:19 +0000516 return ParseDirectiveSymbolAttribute(MCStreamer::Protected);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000517 if (IDVal == ".reference")
Daniel Dunbard7b267b2009-06-30 00:33:19 +0000518 return ParseDirectiveSymbolAttribute(MCStreamer::Reference);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000519 if (IDVal == ".weak")
Daniel Dunbard7b267b2009-06-30 00:33:19 +0000520 return ParseDirectiveSymbolAttribute(MCStreamer::Weak);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000521 if (IDVal == ".weak_definition")
Daniel Dunbard7b267b2009-06-30 00:33:19 +0000522 return ParseDirectiveSymbolAttribute(MCStreamer::WeakDefinition);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000523 if (IDVal == ".weak_reference")
Daniel Dunbard7b267b2009-06-30 00:33:19 +0000524 return ParseDirectiveSymbolAttribute(MCStreamer::WeakReference);
525
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000526 if (IDVal == ".comm")
Chris Lattner1fc3d752009-07-09 17:25:12 +0000527 return ParseDirectiveComm(/*IsLocal=*/false);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000528 if (IDVal == ".lcomm")
Chris Lattner1fc3d752009-07-09 17:25:12 +0000529 return ParseDirectiveComm(/*IsLocal=*/true);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000530 if (IDVal == ".zerofill")
Chris Lattner9be3fee2009-07-10 22:20:30 +0000531 return ParseDirectiveDarwinZerofill();
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000532 if (IDVal == ".desc")
Kevin Enderby95cf30c2009-07-14 18:17:10 +0000533 return ParseDirectiveDarwinSymbolDesc();
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000534 if (IDVal == ".lsym")
Kevin Enderby71148242009-07-14 21:35:03 +0000535 return ParseDirectiveDarwinLsym();
Chris Lattner4e4db7a2009-07-07 20:30:46 +0000536
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000537 if (IDVal == ".subsections_via_symbols")
Kevin Enderbya5c78322009-07-13 21:03:15 +0000538 return ParseDirectiveDarwinSubsectionsViaSymbols();
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000539 if (IDVal == ".abort")
Kevin Enderby5f1f0b82009-07-13 23:15:14 +0000540 return ParseDirectiveAbort();
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000541 if (IDVal == ".include")
Kevin Enderby1f049b22009-07-14 23:21:55 +0000542 return ParseDirectiveInclude();
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000543 if (IDVal == ".dump")
Kevin Enderby5026ae42009-07-20 20:25:37 +0000544 return ParseDirectiveDarwinDumpOrLoad(IDLoc, /*IsDump=*/true);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000545 if (IDVal == ".load")
Kevin Enderby5026ae42009-07-20 20:25:37 +0000546 return ParseDirectiveDarwinDumpOrLoad(IDLoc, /*IsLoad=*/false);
Kevin Enderbya5c78322009-07-13 21:03:15 +0000547
Daniel Dunbar3fb76832009-06-30 00:49:23 +0000548 Warning(IDLoc, "ignoring directive for now");
Chris Lattner2cf5f142009-06-22 01:29:09 +0000549 EatToEndOfStatement();
550 return false;
551 }
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000552
Chris Lattner29dfe7c2009-06-23 18:41:30 +0000553 MCInst Inst;
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000554 if (getTargetParser().ParseInstruction(IDVal, Inst))
Chris Lattner29dfe7c2009-06-23 18:41:30 +0000555 return true;
Chris Lattner2cf5f142009-06-22 01:29:09 +0000556
Daniel Dunbar3f872332009-07-28 16:08:33 +0000557 if (Lexer.isNot(AsmToken::EndOfStatement))
Chris Lattner9a023f72009-06-24 04:43:34 +0000558 return TokError("unexpected token in argument list");
Chris Lattner2cf5f142009-06-22 01:29:09 +0000559
560 // Eat the end of statement marker.
561 Lexer.Lex();
562
563 // Instruction is good, process it.
Daniel Dunbar0eebb052009-07-01 06:35:48 +0000564 Out.EmitInstruction(Inst);
Chris Lattner2cf5f142009-06-22 01:29:09 +0000565
566 // Skip to end of line for now.
Chris Lattner27aa7d22009-06-21 20:16:42 +0000567 return false;
568}
Chris Lattner9a023f72009-06-24 04:43:34 +0000569
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000570bool AsmParser::ParseAssignment(const StringRef &Name, bool IsDotSet) {
Daniel Dunbardce0f3c2009-06-29 23:43:14 +0000571 // FIXME: Use better location, we should use proper tokens.
572 SMLoc EqualLoc = Lexer.getLoc();
573
Daniel Dunbarf4b830f2009-06-30 02:10:03 +0000574 MCValue Value;
575 if (ParseRelocatableExpression(Value))
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000576 return true;
577
Daniel Dunbar3f872332009-07-28 16:08:33 +0000578 if (Lexer.isNot(AsmToken::EndOfStatement))
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000579 return TokError("unexpected token in assignment");
580
581 // Eat the end of statement marker.
582 Lexer.Lex();
583
Daniel Dunbardce0f3c2009-06-29 23:43:14 +0000584 // Diagnose assignment to a label.
585 //
586 // FIXME: Diagnostics. Note the location of the definition as a label.
587 // FIXME: This doesn't diagnose assignment to a symbol which has been
588 // implicitly marked as external.
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000589 // FIXME: Handle '.'.
Daniel Dunbardce0f3c2009-06-29 23:43:14 +0000590 // FIXME: Diagnose assignment to protected identifier (e.g., register name).
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000591 MCSymbol *Sym = Ctx.GetOrCreateSymbol(Name);
Daniel Dunbardce0f3c2009-06-29 23:43:14 +0000592 if (Sym->getSection())
593 return Error(EqualLoc, "invalid assignment to symbol emitted as a label");
594 if (Sym->isExternal())
595 return Error(EqualLoc, "invalid assignment to external symbol");
596
597 // Do the assignment.
Daniel Dunbarf4b830f2009-06-30 02:10:03 +0000598 Out.EmitAssignment(Sym, Value, IsDotSet);
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000599
600 return false;
601}
602
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +0000603/// ParseIdentifier:
604/// ::= identifier
605/// ::= string
606bool AsmParser::ParseIdentifier(StringRef &Res) {
607 if (Lexer.isNot(AsmToken::Identifier) &&
608 Lexer.isNot(AsmToken::String))
609 return true;
610
611 Res = Lexer.getTok().getIdentifier();
612
613 Lexer.Lex(); // Consume the identifier token.
614
615 return false;
616}
617
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000618/// ParseDirectiveSet:
619/// ::= .set identifier ',' expression
620bool AsmParser::ParseDirectiveSet() {
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +0000621 StringRef Name;
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000622
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +0000623 if (ParseIdentifier(Name))
624 return TokError("expected identifier after '.set' directive");
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000625
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +0000626 if (Lexer.isNot(AsmToken::Comma))
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000627 return TokError("unexpected token in '.set'");
628 Lexer.Lex();
629
630 return ParseAssignment(Name, true);
631}
632
Chris Lattner9a023f72009-06-24 04:43:34 +0000633/// ParseDirectiveSection:
Chris Lattner529fb542009-06-24 05:13:15 +0000634/// ::= .section identifier (',' identifier)*
635/// FIXME: This should actually parse out the segment, section, attributes and
636/// sizeof_stub fields.
637bool AsmParser::ParseDirectiveDarwinSection() {
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +0000638 StringRef SectionName;
639
640 if (ParseIdentifier(SectionName))
Chris Lattner9a023f72009-06-24 04:43:34 +0000641 return TokError("expected identifier after '.section' directive");
642
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +0000643 std::string Section = SectionName;
644
645 // FIXME: This doesn't work, we lose quoting on things
646
Chris Lattner9a023f72009-06-24 04:43:34 +0000647 // Accept a comma separated list of modifiers.
Daniel Dunbar3f872332009-07-28 16:08:33 +0000648 while (Lexer.is(AsmToken::Comma)) {
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +0000649 Lexer.Lex(); // Consume the comma.
650
651 StringRef ModifierName;
652 if (ParseIdentifier(ModifierName))
Chris Lattner9a023f72009-06-24 04:43:34 +0000653 return TokError("expected identifier in '.section' directive");
654 Section += ',';
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +0000655 Section += ModifierName;
Chris Lattner9a023f72009-06-24 04:43:34 +0000656 }
657
Daniel Dunbar3f872332009-07-28 16:08:33 +0000658 if (Lexer.isNot(AsmToken::EndOfStatement))
Chris Lattner9a023f72009-06-24 04:43:34 +0000659 return TokError("unexpected token in '.section' directive");
660 Lexer.Lex();
661
Chris Lattner56594f92009-07-31 17:47:16 +0000662 // FIXME: Arch specific.
663 MCSection *S = Ctx.GetSection(Section);
664 if (S == 0)
Chris Lattner5d655422009-08-01 21:14:30 +0000665 S = MCSection::Create(Section, false, SectionKind(), Ctx);
Chris Lattner56594f92009-07-31 17:47:16 +0000666
667 Out.SwitchSection(S);
Chris Lattner9a023f72009-06-24 04:43:34 +0000668 return false;
669}
670
Chris Lattner529fb542009-06-24 05:13:15 +0000671bool AsmParser::ParseDirectiveSectionSwitch(const char *Section,
672 const char *Directives) {
Daniel Dunbar3f872332009-07-28 16:08:33 +0000673 if (Lexer.isNot(AsmToken::EndOfStatement))
Chris Lattner529fb542009-06-24 05:13:15 +0000674 return TokError("unexpected token in section switching directive");
675 Lexer.Lex();
676
677 std::string SectionStr = Section;
678 if (Directives && Directives[0]) {
679 SectionStr += ",";
680 SectionStr += Directives;
681 }
682
Chris Lattner56594f92009-07-31 17:47:16 +0000683 // FIXME: Arch specific.
684 MCSection *S = Ctx.GetSection(Section);
685 if (S == 0)
Chris Lattner5d655422009-08-01 21:14:30 +0000686 S = MCSection::Create(Section, false, SectionKind(), Ctx);
Chris Lattner56594f92009-07-31 17:47:16 +0000687
688 Out.SwitchSection(S);
Chris Lattner529fb542009-06-24 05:13:15 +0000689 return false;
690}
Daniel Dunbara0d14262009-06-24 23:30:00 +0000691
692/// ParseDirectiveAscii:
Daniel Dunbar475839e2009-06-29 20:37:27 +0000693/// ::= ( .ascii | .asciz ) [ "string" ( , "string" )* ]
Daniel Dunbara0d14262009-06-24 23:30:00 +0000694bool AsmParser::ParseDirectiveAscii(bool ZeroTerminated) {
Daniel Dunbar3f872332009-07-28 16:08:33 +0000695 if (Lexer.isNot(AsmToken::EndOfStatement)) {
Daniel Dunbara0d14262009-06-24 23:30:00 +0000696 for (;;) {
Daniel Dunbar3f872332009-07-28 16:08:33 +0000697 if (Lexer.isNot(AsmToken::String))
Daniel Dunbara0d14262009-06-24 23:30:00 +0000698 return TokError("expected string in '.ascii' or '.asciz' directive");
699
700 // FIXME: This shouldn't use a const char* + strlen, the string could have
701 // embedded nulls.
702 // FIXME: Should have accessor for getting string contents.
Daniel Dunbar419aded2009-07-28 16:38:40 +0000703 StringRef Str = Lexer.getTok().getString();
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000704 Out.EmitBytes(Str.substr(1, Str.size() - 2));
Daniel Dunbara0d14262009-06-24 23:30:00 +0000705 if (ZeroTerminated)
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000706 Out.EmitBytes(StringRef("\0", 1));
Daniel Dunbara0d14262009-06-24 23:30:00 +0000707
708 Lexer.Lex();
709
Daniel Dunbar3f872332009-07-28 16:08:33 +0000710 if (Lexer.is(AsmToken::EndOfStatement))
Daniel Dunbara0d14262009-06-24 23:30:00 +0000711 break;
712
Daniel Dunbar3f872332009-07-28 16:08:33 +0000713 if (Lexer.isNot(AsmToken::Comma))
Daniel Dunbara0d14262009-06-24 23:30:00 +0000714 return TokError("unexpected token in '.ascii' or '.asciz' directive");
715 Lexer.Lex();
716 }
717 }
718
719 Lexer.Lex();
720 return false;
721}
722
723/// ParseDirectiveValue
724/// ::= (.byte | .short | ... ) [ expression (, expression)* ]
725bool AsmParser::ParseDirectiveValue(unsigned Size) {
Daniel Dunbar3f872332009-07-28 16:08:33 +0000726 if (Lexer.isNot(AsmToken::EndOfStatement)) {
Daniel Dunbara0d14262009-06-24 23:30:00 +0000727 for (;;) {
Daniel Dunbarf4b830f2009-06-30 02:10:03 +0000728 MCValue Expr;
729 if (ParseRelocatableExpression(Expr))
Daniel Dunbara0d14262009-06-24 23:30:00 +0000730 return true;
731
Daniel Dunbarf4b830f2009-06-30 02:10:03 +0000732 Out.EmitValue(Expr, Size);
Daniel Dunbara0d14262009-06-24 23:30:00 +0000733
Daniel Dunbar3f872332009-07-28 16:08:33 +0000734 if (Lexer.is(AsmToken::EndOfStatement))
Daniel Dunbara0d14262009-06-24 23:30:00 +0000735 break;
736
737 // FIXME: Improve diagnostic.
Daniel Dunbar3f872332009-07-28 16:08:33 +0000738 if (Lexer.isNot(AsmToken::Comma))
Daniel Dunbara0d14262009-06-24 23:30:00 +0000739 return TokError("unexpected token in directive");
740 Lexer.Lex();
741 }
742 }
743
744 Lexer.Lex();
745 return false;
746}
747
748/// ParseDirectiveSpace
749/// ::= .space expression [ , expression ]
750bool AsmParser::ParseDirectiveSpace() {
751 int64_t NumBytes;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000752 if (ParseAbsoluteExpression(NumBytes))
Daniel Dunbara0d14262009-06-24 23:30:00 +0000753 return true;
754
755 int64_t FillExpr = 0;
756 bool HasFillExpr = false;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000757 if (Lexer.isNot(AsmToken::EndOfStatement)) {
758 if (Lexer.isNot(AsmToken::Comma))
Daniel Dunbara0d14262009-06-24 23:30:00 +0000759 return TokError("unexpected token in '.space' directive");
760 Lexer.Lex();
761
Daniel Dunbar475839e2009-06-29 20:37:27 +0000762 if (ParseAbsoluteExpression(FillExpr))
Daniel Dunbara0d14262009-06-24 23:30:00 +0000763 return true;
764
765 HasFillExpr = true;
766
Daniel Dunbar3f872332009-07-28 16:08:33 +0000767 if (Lexer.isNot(AsmToken::EndOfStatement))
Daniel Dunbara0d14262009-06-24 23:30:00 +0000768 return TokError("unexpected token in '.space' directive");
769 }
770
771 Lexer.Lex();
772
773 if (NumBytes <= 0)
774 return TokError("invalid number of bytes in '.space' directive");
775
776 // FIXME: Sometimes the fill expr is 'nop' if it isn't supplied, instead of 0.
777 for (uint64_t i = 0, e = NumBytes; i != e; ++i)
778 Out.EmitValue(MCValue::get(FillExpr), 1);
779
780 return false;
781}
782
783/// ParseDirectiveFill
784/// ::= .fill expression , expression , expression
785bool AsmParser::ParseDirectiveFill() {
786 int64_t NumValues;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000787 if (ParseAbsoluteExpression(NumValues))
Daniel Dunbara0d14262009-06-24 23:30:00 +0000788 return true;
789
Daniel Dunbar3f872332009-07-28 16:08:33 +0000790 if (Lexer.isNot(AsmToken::Comma))
Daniel Dunbara0d14262009-06-24 23:30:00 +0000791 return TokError("unexpected token in '.fill' directive");
792 Lexer.Lex();
793
794 int64_t FillSize;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000795 if (ParseAbsoluteExpression(FillSize))
Daniel Dunbara0d14262009-06-24 23:30:00 +0000796 return true;
797
Daniel Dunbar3f872332009-07-28 16:08:33 +0000798 if (Lexer.isNot(AsmToken::Comma))
Daniel Dunbara0d14262009-06-24 23:30:00 +0000799 return TokError("unexpected token in '.fill' directive");
800 Lexer.Lex();
801
802 int64_t FillExpr;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000803 if (ParseAbsoluteExpression(FillExpr))
Daniel Dunbara0d14262009-06-24 23:30:00 +0000804 return true;
805
Daniel Dunbar3f872332009-07-28 16:08:33 +0000806 if (Lexer.isNot(AsmToken::EndOfStatement))
Daniel Dunbara0d14262009-06-24 23:30:00 +0000807 return TokError("unexpected token in '.fill' directive");
808
809 Lexer.Lex();
810
811 if (FillSize != 1 && FillSize != 2 && FillSize != 4)
812 return TokError("invalid '.fill' size, expected 1, 2, or 4");
813
814 for (uint64_t i = 0, e = NumValues; i != e; ++i)
815 Out.EmitValue(MCValue::get(FillExpr), FillSize);
816
817 return false;
818}
Daniel Dunbarc238b582009-06-25 22:44:51 +0000819
820/// ParseDirectiveOrg
821/// ::= .org expression [ , expression ]
822bool AsmParser::ParseDirectiveOrg() {
Daniel Dunbarf4b830f2009-06-30 02:10:03 +0000823 MCValue Offset;
824 if (ParseRelocatableExpression(Offset))
Daniel Dunbarc238b582009-06-25 22:44:51 +0000825 return true;
826
827 // Parse optional fill expression.
828 int64_t FillExpr = 0;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000829 if (Lexer.isNot(AsmToken::EndOfStatement)) {
830 if (Lexer.isNot(AsmToken::Comma))
Daniel Dunbarc238b582009-06-25 22:44:51 +0000831 return TokError("unexpected token in '.org' directive");
832 Lexer.Lex();
833
Daniel Dunbar475839e2009-06-29 20:37:27 +0000834 if (ParseAbsoluteExpression(FillExpr))
Daniel Dunbarc238b582009-06-25 22:44:51 +0000835 return true;
836
Daniel Dunbar3f872332009-07-28 16:08:33 +0000837 if (Lexer.isNot(AsmToken::EndOfStatement))
Daniel Dunbarc238b582009-06-25 22:44:51 +0000838 return TokError("unexpected token in '.org' directive");
839 }
840
841 Lexer.Lex();
Daniel Dunbarf4b830f2009-06-30 02:10:03 +0000842
843 // FIXME: Only limited forms of relocatable expressions are accepted here, it
844 // has to be relative to the current section.
845 Out.EmitValueToOffset(Offset, FillExpr);
Daniel Dunbarc238b582009-06-25 22:44:51 +0000846
847 return false;
848}
Daniel Dunbarc29dfa72009-06-29 23:46:59 +0000849
850/// ParseDirectiveAlign
851/// ::= {.align, ...} expression [ , expression [ , expression ]]
852bool AsmParser::ParseDirectiveAlign(bool IsPow2, unsigned ValueSize) {
853 int64_t Alignment;
854 if (ParseAbsoluteExpression(Alignment))
855 return true;
856
857 SMLoc MaxBytesLoc;
858 bool HasFillExpr = false;
859 int64_t FillExpr = 0;
860 int64_t MaxBytesToFill = 0;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000861 if (Lexer.isNot(AsmToken::EndOfStatement)) {
862 if (Lexer.isNot(AsmToken::Comma))
Daniel Dunbarc29dfa72009-06-29 23:46:59 +0000863 return TokError("unexpected token in directive");
864 Lexer.Lex();
865
866 // The fill expression can be omitted while specifying a maximum number of
867 // alignment bytes, e.g:
868 // .align 3,,4
Daniel Dunbar3f872332009-07-28 16:08:33 +0000869 if (Lexer.isNot(AsmToken::Comma)) {
Daniel Dunbarc29dfa72009-06-29 23:46:59 +0000870 HasFillExpr = true;
871 if (ParseAbsoluteExpression(FillExpr))
872 return true;
873 }
874
Daniel Dunbar3f872332009-07-28 16:08:33 +0000875 if (Lexer.isNot(AsmToken::EndOfStatement)) {
876 if (Lexer.isNot(AsmToken::Comma))
Daniel Dunbarc29dfa72009-06-29 23:46:59 +0000877 return TokError("unexpected token in directive");
878 Lexer.Lex();
879
880 MaxBytesLoc = Lexer.getLoc();
881 if (ParseAbsoluteExpression(MaxBytesToFill))
882 return true;
883
Daniel Dunbar3f872332009-07-28 16:08:33 +0000884 if (Lexer.isNot(AsmToken::EndOfStatement))
Daniel Dunbarc29dfa72009-06-29 23:46:59 +0000885 return TokError("unexpected token in directive");
886 }
887 }
888
889 Lexer.Lex();
890
891 if (!HasFillExpr) {
892 // FIXME: Sometimes fill with nop.
893 FillExpr = 0;
894 }
895
896 // Compute alignment in bytes.
897 if (IsPow2) {
898 // FIXME: Diagnose overflow.
Chris Lattner39750252009-07-11 22:32:37 +0000899 Alignment = 1LL << Alignment;
Daniel Dunbarc29dfa72009-06-29 23:46:59 +0000900 }
901
902 // Diagnose non-sensical max bytes to fill.
903 if (MaxBytesLoc.isValid()) {
904 if (MaxBytesToFill < 1) {
Daniel Dunbar3fb76832009-06-30 00:49:23 +0000905 Warning(MaxBytesLoc, "alignment directive can never be satisfied in this "
906 "many bytes, ignoring");
Daniel Dunbarc29dfa72009-06-29 23:46:59 +0000907 return false;
908 }
909
910 if (MaxBytesToFill >= Alignment) {
Daniel Dunbar3fb76832009-06-30 00:49:23 +0000911 Warning(MaxBytesLoc, "maximum bytes expression exceeds alignment and "
912 "has no effect");
Daniel Dunbarc29dfa72009-06-29 23:46:59 +0000913 MaxBytesToFill = 0;
914 }
915 }
916
917 // FIXME: Target specific behavior about how the "extra" bytes are filled.
918 Out.EmitValueToAlignment(Alignment, FillExpr, ValueSize, MaxBytesToFill);
919
920 return false;
921}
922
Daniel Dunbard7b267b2009-06-30 00:33:19 +0000923/// ParseDirectiveSymbolAttribute
924/// ::= { ".globl", ".weak", ... } [ identifier ( , identifier )* ]
925bool AsmParser::ParseDirectiveSymbolAttribute(MCStreamer::SymbolAttr Attr) {
Daniel Dunbar3f872332009-07-28 16:08:33 +0000926 if (Lexer.isNot(AsmToken::EndOfStatement)) {
Daniel Dunbard7b267b2009-06-30 00:33:19 +0000927 for (;;) {
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +0000928 StringRef Name;
929
930 if (ParseIdentifier(Name))
Daniel Dunbard7b267b2009-06-30 00:33:19 +0000931 return TokError("expected identifier in directive");
932
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +0000933 MCSymbol *Sym = Ctx.GetOrCreateSymbol(Name);
Daniel Dunbard7b267b2009-06-30 00:33:19 +0000934
935 // If this is use of an undefined symbol then mark it external.
936 if (!Sym->getSection() && !Ctx.GetSymbolValue(Sym))
937 Sym->setExternal(true);
938
939 Out.EmitSymbolAttribute(Sym, Attr);
940
Daniel Dunbar3f872332009-07-28 16:08:33 +0000941 if (Lexer.is(AsmToken::EndOfStatement))
Daniel Dunbard7b267b2009-06-30 00:33:19 +0000942 break;
943
Daniel Dunbar3f872332009-07-28 16:08:33 +0000944 if (Lexer.isNot(AsmToken::Comma))
Daniel Dunbard7b267b2009-06-30 00:33:19 +0000945 return TokError("unexpected token in directive");
946 Lexer.Lex();
947 }
948 }
949
950 Lexer.Lex();
951 return false;
952}
Chris Lattner4e4db7a2009-07-07 20:30:46 +0000953
Kevin Enderby95cf30c2009-07-14 18:17:10 +0000954/// ParseDirectiveDarwinSymbolDesc
955/// ::= .desc identifier , expression
956bool AsmParser::ParseDirectiveDarwinSymbolDesc() {
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +0000957 StringRef Name;
958 if (ParseIdentifier(Name))
Kevin Enderby95cf30c2009-07-14 18:17:10 +0000959 return TokError("expected identifier in directive");
960
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +0000961 // Handle the identifier as the key symbol.
962 MCSymbol *Sym = Ctx.GetOrCreateSymbol(Name);
Kevin Enderby95cf30c2009-07-14 18:17:10 +0000963
Daniel Dunbar3f872332009-07-28 16:08:33 +0000964 if (Lexer.isNot(AsmToken::Comma))
Kevin Enderby95cf30c2009-07-14 18:17:10 +0000965 return TokError("unexpected token in '.desc' directive");
966 Lexer.Lex();
967
968 SMLoc DescLoc = Lexer.getLoc();
969 int64_t DescValue;
970 if (ParseAbsoluteExpression(DescValue))
971 return true;
972
Daniel Dunbar3f872332009-07-28 16:08:33 +0000973 if (Lexer.isNot(AsmToken::EndOfStatement))
Kevin Enderby95cf30c2009-07-14 18:17:10 +0000974 return TokError("unexpected token in '.desc' directive");
975
976 Lexer.Lex();
977
978 // Set the n_desc field of this Symbol to this DescValue
979 Out.EmitSymbolDesc(Sym, DescValue);
980
981 return false;
982}
983
Chris Lattner4e4db7a2009-07-07 20:30:46 +0000984/// ParseDirectiveComm
Chris Lattner1fc3d752009-07-09 17:25:12 +0000985/// ::= ( .comm | .lcomm ) identifier , size_expression [ , align_expression ]
986bool AsmParser::ParseDirectiveComm(bool IsLocal) {
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +0000987 SMLoc IDLoc = Lexer.getLoc();
988 StringRef Name;
989 if (ParseIdentifier(Name))
Chris Lattner4e4db7a2009-07-07 20:30:46 +0000990 return TokError("expected identifier in directive");
991
Daniel Dunbar76c4d762009-07-31 21:55:09 +0000992 // Handle the identifier as the key symbol.
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +0000993 MCSymbol *Sym = Ctx.GetOrCreateSymbol(Name);
Chris Lattner4e4db7a2009-07-07 20:30:46 +0000994
Daniel Dunbar3f872332009-07-28 16:08:33 +0000995 if (Lexer.isNot(AsmToken::Comma))
Chris Lattner4e4db7a2009-07-07 20:30:46 +0000996 return TokError("unexpected token in directive");
997 Lexer.Lex();
998
999 int64_t Size;
1000 SMLoc SizeLoc = Lexer.getLoc();
1001 if (ParseAbsoluteExpression(Size))
1002 return true;
1003
1004 int64_t Pow2Alignment = 0;
1005 SMLoc Pow2AlignmentLoc;
Daniel Dunbar3f872332009-07-28 16:08:33 +00001006 if (Lexer.is(AsmToken::Comma)) {
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001007 Lexer.Lex();
1008 Pow2AlignmentLoc = Lexer.getLoc();
1009 if (ParseAbsoluteExpression(Pow2Alignment))
1010 return true;
1011 }
1012
Daniel Dunbar3f872332009-07-28 16:08:33 +00001013 if (Lexer.isNot(AsmToken::EndOfStatement))
Chris Lattner1fc3d752009-07-09 17:25:12 +00001014 return TokError("unexpected token in '.comm' or '.lcomm' directive");
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001015
1016 Lexer.Lex();
1017
Chris Lattner1fc3d752009-07-09 17:25:12 +00001018 // NOTE: a size of zero for a .comm should create a undefined symbol
1019 // but a size of .lcomm creates a bss symbol of size zero.
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001020 if (Size < 0)
Chris Lattner1fc3d752009-07-09 17:25:12 +00001021 return Error(SizeLoc, "invalid '.comm' or '.lcomm' directive size, can't "
1022 "be less than zero");
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001023
1024 // NOTE: The alignment in the directive is a power of 2 value, the assember
1025 // may internally end up wanting an alignment in bytes.
1026 // FIXME: Diagnose overflow.
1027 if (Pow2Alignment < 0)
Chris Lattner1fc3d752009-07-09 17:25:12 +00001028 return Error(Pow2AlignmentLoc, "invalid '.comm' or '.lcomm' directive "
1029 "alignment, can't be less than zero");
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001030
1031 // TODO: Symbol must be undefined or it is a error to re-defined the symbol
1032 if (Sym->getSection() || Ctx.GetSymbolValue(Sym))
1033 return Error(IDLoc, "invalid symbol redefinition");
1034
Chris Lattner1fc3d752009-07-09 17:25:12 +00001035 // Create the Symbol as a common or local common with Size and Pow2Alignment
1036 Out.EmitCommonSymbol(Sym, Size, Pow2Alignment, IsLocal);
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001037
1038 return false;
1039}
Chris Lattner9be3fee2009-07-10 22:20:30 +00001040
1041/// ParseDirectiveDarwinZerofill
1042/// ::= .zerofill segname , sectname [, identifier , size_expression [
1043/// , align_expression ]]
1044bool AsmParser::ParseDirectiveDarwinZerofill() {
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00001045 // FIXME: Handle quoted names here.
1046
Daniel Dunbar3f872332009-07-28 16:08:33 +00001047 if (Lexer.isNot(AsmToken::Identifier))
Chris Lattner9be3fee2009-07-10 22:20:30 +00001048 return TokError("expected segment name after '.zerofill' directive");
Daniel Dunbar419aded2009-07-28 16:38:40 +00001049 std::string Section = Lexer.getTok().getString();
Chris Lattner9be3fee2009-07-10 22:20:30 +00001050 Lexer.Lex();
1051
Daniel Dunbar3f872332009-07-28 16:08:33 +00001052 if (Lexer.isNot(AsmToken::Comma))
Chris Lattner9be3fee2009-07-10 22:20:30 +00001053 return TokError("unexpected token in directive");
1054 Section += ',';
1055 Lexer.Lex();
1056
Daniel Dunbar3f872332009-07-28 16:08:33 +00001057 if (Lexer.isNot(AsmToken::Identifier))
Chris Lattner9be3fee2009-07-10 22:20:30 +00001058 return TokError("expected section name after comma in '.zerofill' "
1059 "directive");
Daniel Dunbar419aded2009-07-28 16:38:40 +00001060 Section += Lexer.getTok().getString().str();
Chris Lattner9be3fee2009-07-10 22:20:30 +00001061 Lexer.Lex();
1062
1063 // FIXME: we will need to tell GetSection() that this is to be created with or
1064 // must have the Mach-O section type of S_ZEROFILL. Something like the code
1065 // below could be done but for now it is not as EmitZerofill() does not know
1066 // how to deal with a section type in the section name like
1067 // ParseDirectiveDarwinSection() allows.
1068 // Section += ',';
1069 // Section += "zerofill";
1070
1071 // If this is the end of the line all that was wanted was to create the
1072 // the section but with no symbol.
Daniel Dunbar3f872332009-07-28 16:08:33 +00001073 if (Lexer.is(AsmToken::EndOfStatement)) {
Chris Lattner6bdd74c2009-07-31 18:27:48 +00001074 // FIXME: Arch specific.
1075 MCSection *S = Ctx.GetSection(Section);
1076 if (S == 0)
Chris Lattner5d655422009-08-01 21:14:30 +00001077 S = MCSection::Create(Section, false, SectionKind(), Ctx);
Chris Lattner6bdd74c2009-07-31 18:27:48 +00001078
Chris Lattner9be3fee2009-07-10 22:20:30 +00001079 // Create the zerofill section but no symbol
Chris Lattner6bdd74c2009-07-31 18:27:48 +00001080 Out.EmitZerofill(S);
Chris Lattner9be3fee2009-07-10 22:20:30 +00001081 return false;
1082 }
1083
Daniel Dunbar3f872332009-07-28 16:08:33 +00001084 if (Lexer.isNot(AsmToken::Comma))
Chris Lattner9be3fee2009-07-10 22:20:30 +00001085 return TokError("unexpected token in directive");
1086 Lexer.Lex();
1087
Daniel Dunbar3f872332009-07-28 16:08:33 +00001088 if (Lexer.isNot(AsmToken::Identifier))
Chris Lattner9be3fee2009-07-10 22:20:30 +00001089 return TokError("expected identifier in directive");
1090
1091 // handle the identifier as the key symbol.
1092 SMLoc IDLoc = Lexer.getLoc();
Daniel Dunbar419aded2009-07-28 16:38:40 +00001093 MCSymbol *Sym = Ctx.GetOrCreateSymbol(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 Lexer.Lex();
1099
1100 int64_t Size;
1101 SMLoc SizeLoc = Lexer.getLoc();
1102 if (ParseAbsoluteExpression(Size))
1103 return true;
1104
1105 int64_t Pow2Alignment = 0;
1106 SMLoc Pow2AlignmentLoc;
Daniel Dunbar3f872332009-07-28 16:08:33 +00001107 if (Lexer.is(AsmToken::Comma)) {
Chris Lattner9be3fee2009-07-10 22:20:30 +00001108 Lexer.Lex();
1109 Pow2AlignmentLoc = Lexer.getLoc();
1110 if (ParseAbsoluteExpression(Pow2Alignment))
1111 return true;
1112 }
1113
Daniel Dunbar3f872332009-07-28 16:08:33 +00001114 if (Lexer.isNot(AsmToken::EndOfStatement))
Chris Lattner9be3fee2009-07-10 22:20:30 +00001115 return TokError("unexpected token in '.zerofill' directive");
1116
1117 Lexer.Lex();
1118
1119 if (Size < 0)
1120 return Error(SizeLoc, "invalid '.zerofill' directive size, can't be less "
1121 "than zero");
1122
1123 // NOTE: The alignment in the directive is a power of 2 value, the assember
1124 // may internally end up wanting an alignment in bytes.
1125 // FIXME: Diagnose overflow.
1126 if (Pow2Alignment < 0)
1127 return Error(Pow2AlignmentLoc, "invalid '.zerofill' directive alignment, "
1128 "can't be less than zero");
1129
1130 // TODO: Symbol must be undefined or it is a error to re-defined the symbol
1131 if (Sym->getSection() || Ctx.GetSymbolValue(Sym))
1132 return Error(IDLoc, "invalid symbol redefinition");
1133
Chris Lattner6bdd74c2009-07-31 18:27:48 +00001134 // FIXME: Arch specific.
1135 MCSection *S = Ctx.GetSection(Section);
1136 if (S == 0)
Chris Lattner5d655422009-08-01 21:14:30 +00001137 S = MCSection::Create(Section, false, SectionKind(), Ctx);
Chris Lattner6bdd74c2009-07-31 18:27:48 +00001138
Chris Lattner9be3fee2009-07-10 22:20:30 +00001139 // Create the zerofill Symbol with Size and Pow2Alignment
Chris Lattner6bdd74c2009-07-31 18:27:48 +00001140 Out.EmitZerofill(S, Sym, Size, Pow2Alignment);
Chris Lattner9be3fee2009-07-10 22:20:30 +00001141
1142 return false;
1143}
Kevin Enderbya5c78322009-07-13 21:03:15 +00001144
1145/// ParseDirectiveDarwinSubsectionsViaSymbols
1146/// ::= .subsections_via_symbols
1147bool AsmParser::ParseDirectiveDarwinSubsectionsViaSymbols() {
Daniel Dunbar3f872332009-07-28 16:08:33 +00001148 if (Lexer.isNot(AsmToken::EndOfStatement))
Kevin Enderbya5c78322009-07-13 21:03:15 +00001149 return TokError("unexpected token in '.subsections_via_symbols' directive");
1150
1151 Lexer.Lex();
1152
Kevin Enderbyf96db462009-07-16 17:56:39 +00001153 Out.EmitAssemblerFlag(MCStreamer::SubsectionsViaSymbols);
Kevin Enderbya5c78322009-07-13 21:03:15 +00001154
1155 return false;
1156}
Kevin Enderby5f1f0b82009-07-13 23:15:14 +00001157
1158/// ParseDirectiveAbort
1159/// ::= .abort [ "abort_string" ]
1160bool AsmParser::ParseDirectiveAbort() {
Daniel Dunbarf9507ff2009-07-27 23:20:52 +00001161 // FIXME: Use loc from directive.
1162 SMLoc Loc = Lexer.getLoc();
1163
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001164 StringRef Str = "";
Daniel Dunbar3f872332009-07-28 16:08:33 +00001165 if (Lexer.isNot(AsmToken::EndOfStatement)) {
1166 if (Lexer.isNot(AsmToken::String))
Kevin Enderby5f1f0b82009-07-13 23:15:14 +00001167 return TokError("expected string in '.abort' directive");
1168
Daniel Dunbar419aded2009-07-28 16:38:40 +00001169 Str = Lexer.getTok().getString();
Kevin Enderby5f1f0b82009-07-13 23:15:14 +00001170
1171 Lexer.Lex();
1172 }
1173
Daniel Dunbar3f872332009-07-28 16:08:33 +00001174 if (Lexer.isNot(AsmToken::EndOfStatement))
Kevin Enderby5f1f0b82009-07-13 23:15:14 +00001175 return TokError("unexpected token in '.abort' directive");
1176
1177 Lexer.Lex();
1178
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001179 // FIXME: Handle here.
Daniel Dunbarf9507ff2009-07-27 23:20:52 +00001180 if (Str.empty())
1181 Error(Loc, ".abort detected. Assembly stopping.");
1182 else
1183 Error(Loc, ".abort '" + Str + "' detected. Assembly stopping.");
Kevin Enderby5f1f0b82009-07-13 23:15:14 +00001184
1185 return false;
1186}
Kevin Enderby71148242009-07-14 21:35:03 +00001187
1188/// ParseDirectiveLsym
1189/// ::= .lsym identifier , expression
1190bool AsmParser::ParseDirectiveDarwinLsym() {
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00001191 StringRef Name;
1192 if (ParseIdentifier(Name))
Kevin Enderby71148242009-07-14 21:35:03 +00001193 return TokError("expected identifier in directive");
1194
Daniel Dunbar76c4d762009-07-31 21:55:09 +00001195 // Handle the identifier as the key symbol.
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00001196 MCSymbol *Sym = Ctx.GetOrCreateSymbol(Name);
Kevin Enderby71148242009-07-14 21:35:03 +00001197
Daniel Dunbar3f872332009-07-28 16:08:33 +00001198 if (Lexer.isNot(AsmToken::Comma))
Kevin Enderby71148242009-07-14 21:35:03 +00001199 return TokError("unexpected token in '.lsym' directive");
1200 Lexer.Lex();
1201
1202 MCValue Expr;
1203 if (ParseRelocatableExpression(Expr))
1204 return true;
1205
Daniel Dunbar3f872332009-07-28 16:08:33 +00001206 if (Lexer.isNot(AsmToken::EndOfStatement))
Kevin Enderby71148242009-07-14 21:35:03 +00001207 return TokError("unexpected token in '.lsym' directive");
1208
1209 Lexer.Lex();
1210
1211 // Create the Sym with the value of the Expr
1212 Out.EmitLocalSymbol(Sym, Expr);
1213
1214 return false;
1215}
Kevin Enderby1f049b22009-07-14 23:21:55 +00001216
1217/// ParseDirectiveInclude
1218/// ::= .include "filename"
1219bool AsmParser::ParseDirectiveInclude() {
Daniel Dunbar3f872332009-07-28 16:08:33 +00001220 if (Lexer.isNot(AsmToken::String))
Kevin Enderby1f049b22009-07-14 23:21:55 +00001221 return TokError("expected string in '.include' directive");
1222
Daniel Dunbar419aded2009-07-28 16:38:40 +00001223 std::string Filename = Lexer.getTok().getString();
Chris Lattner8e25e2d2009-07-16 06:14:39 +00001224 SMLoc IncludeLoc = Lexer.getLoc();
Kevin Enderby1f049b22009-07-14 23:21:55 +00001225 Lexer.Lex();
1226
Daniel Dunbar3f872332009-07-28 16:08:33 +00001227 if (Lexer.isNot(AsmToken::EndOfStatement))
Kevin Enderby1f049b22009-07-14 23:21:55 +00001228 return TokError("unexpected token in '.include' directive");
1229
Chris Lattner8e25e2d2009-07-16 06:14:39 +00001230 // Strip the quotes.
1231 Filename = Filename.substr(1, Filename.size()-2);
1232
1233 // Attempt to switch the lexer to the included file before consuming the end
1234 // of statement to avoid losing it when we switch.
1235 if (Lexer.EnterIncludeFile(Filename)) {
1236 Lexer.PrintMessage(IncludeLoc,
1237 "Could not find include file '" + Filename + "'",
1238 "error");
1239 return true;
1240 }
Kevin Enderby1f049b22009-07-14 23:21:55 +00001241
1242 return false;
1243}
Kevin Enderby6e68cd92009-07-15 15:30:11 +00001244
1245/// ParseDirectiveDarwinDumpOrLoad
1246/// ::= ( .dump | .load ) "filename"
Kevin Enderby5026ae42009-07-20 20:25:37 +00001247bool AsmParser::ParseDirectiveDarwinDumpOrLoad(SMLoc IDLoc, bool IsDump) {
Daniel Dunbar3f872332009-07-28 16:08:33 +00001248 if (Lexer.isNot(AsmToken::String))
Kevin Enderby6e68cd92009-07-15 15:30:11 +00001249 return TokError("expected string in '.dump' or '.load' directive");
1250
Kevin Enderby6e68cd92009-07-15 15:30:11 +00001251 Lexer.Lex();
1252
Daniel Dunbar3f872332009-07-28 16:08:33 +00001253 if (Lexer.isNot(AsmToken::EndOfStatement))
Kevin Enderby6e68cd92009-07-15 15:30:11 +00001254 return TokError("unexpected token in '.dump' or '.load' directive");
1255
1256 Lexer.Lex();
1257
Kevin Enderby5026ae42009-07-20 20:25:37 +00001258 // FIXME: If/when .dump and .load are implemented they will be done in the
1259 // the assembly parser and not have any need for an MCStreamer API.
Kevin Enderby6e68cd92009-07-15 15:30:11 +00001260 if (IsDump)
Kevin Enderby5026ae42009-07-20 20:25:37 +00001261 Warning(IDLoc, "ignoring directive .dump for now");
Kevin Enderby6e68cd92009-07-15 15:30:11 +00001262 else
Kevin Enderby5026ae42009-07-20 20:25:37 +00001263 Warning(IDLoc, "ignoring directive .load for now");
Kevin Enderby6e68cd92009-07-15 15:30:11 +00001264
1265 return false;
1266}