blob: 8348479240d803036d435dad721824e90b5dc28f [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 Dunbar3f872332009-07-28 16:08:33 +0000100 case AsmToken::Identifier: {
Chris Lattnerc4193832009-06-22 05:51:26 +0000101 // This is a label, this should be parsed as part of an expression, to
Daniel Dunbar475839e2009-06-29 20:37:27 +0000102 // handle things like LFOO+4.
Daniel Dunbar419aded2009-07-28 16:38:40 +0000103 MCSymbol *Sym = Ctx.GetOrCreateSymbol(Lexer.getTok().getString());
Daniel Dunbardce0f3c2009-06-29 23:43:14 +0000104
105 // If this is use of an undefined symbol then mark it external.
106 if (!Sym->getSection() && !Ctx.GetSymbolValue(Sym))
107 Sym->setExternal(true);
108
109 Res = new AsmSymbolRefExpr(Sym);
Chris Lattnerc4193832009-06-22 05:51:26 +0000110 Lexer.Lex(); // Eat identifier.
111 return false;
Daniel Dunbardce0f3c2009-06-29 23:43:14 +0000112 }
Daniel Dunbar3f872332009-07-28 16:08:33 +0000113 case AsmToken::Integer:
Daniel Dunbar419aded2009-07-28 16:38:40 +0000114 Res = new AsmConstantExpr(Lexer.getTok().getIntVal());
Chris Lattnerc4193832009-06-22 05:51:26 +0000115 Lexer.Lex(); // Eat identifier.
116 return false;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000117 case AsmToken::LParen:
Chris Lattner74ec1a32009-06-22 06:32:03 +0000118 Lexer.Lex(); // Eat the '('.
119 return ParseParenExpr(Res);
Daniel Dunbar3f872332009-07-28 16:08:33 +0000120 case AsmToken::Minus:
Chris Lattner74ec1a32009-06-22 06:32:03 +0000121 Lexer.Lex(); // Eat the operator.
Daniel Dunbar475839e2009-06-29 20:37:27 +0000122 if (ParsePrimaryExpr(Res))
123 return true;
124 Res = new AsmUnaryExpr(AsmUnaryExpr::Minus, Res);
125 return false;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000126 case AsmToken::Plus:
Daniel Dunbar475839e2009-06-29 20:37:27 +0000127 Lexer.Lex(); // Eat the operator.
128 if (ParsePrimaryExpr(Res))
129 return true;
130 Res = new AsmUnaryExpr(AsmUnaryExpr::Plus, Res);
131 return false;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000132 case AsmToken::Tilde:
Daniel Dunbar475839e2009-06-29 20:37:27 +0000133 Lexer.Lex(); // Eat the operator.
134 if (ParsePrimaryExpr(Res))
135 return true;
136 Res = new AsmUnaryExpr(AsmUnaryExpr::Not, Res);
137 return false;
Chris Lattnerc4193832009-06-22 05:51:26 +0000138 }
139}
Chris Lattner74ec1a32009-06-22 06:32:03 +0000140
141/// ParseExpression - Parse an expression and return it.
142///
143/// expr ::= expr +,- expr -> lowest.
144/// expr ::= expr |,^,&,! expr -> middle.
145/// expr ::= expr *,/,%,<<,>> expr -> highest.
146/// expr ::= primaryexpr
147///
Daniel Dunbar475839e2009-06-29 20:37:27 +0000148bool AsmParser::ParseExpression(AsmExpr *&Res) {
149 Res = 0;
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000150 return ParsePrimaryExpr(Res) ||
151 ParseBinOpRHS(1, Res);
Chris Lattner74ec1a32009-06-22 06:32:03 +0000152}
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000153
Daniel Dunbar475839e2009-06-29 20:37:27 +0000154bool AsmParser::ParseAbsoluteExpression(int64_t &Res) {
155 AsmExpr *Expr;
156
Daniel Dunbarf4b830f2009-06-30 02:10:03 +0000157 SMLoc StartLoc = Lexer.getLoc();
Daniel Dunbar475839e2009-06-29 20:37:27 +0000158 if (ParseExpression(Expr))
159 return true;
160
161 if (!Expr->EvaluateAsAbsolute(Ctx, Res))
Daniel Dunbarf4b830f2009-06-30 02:10:03 +0000162 return Error(StartLoc, "expected absolute expression");
Daniel Dunbar475839e2009-06-29 20:37:27 +0000163
164 return false;
165}
166
Daniel Dunbar15d17072009-06-30 01:49:52 +0000167bool AsmParser::ParseRelocatableExpression(MCValue &Res) {
168 AsmExpr *Expr;
169
Daniel Dunbarf4b830f2009-06-30 02:10:03 +0000170 SMLoc StartLoc = Lexer.getLoc();
Daniel Dunbar15d17072009-06-30 01:49:52 +0000171 if (ParseExpression(Expr))
172 return true;
173
174 if (!Expr->EvaluateAsRelocatable(Ctx, Res))
Daniel Dunbarf4b830f2009-06-30 02:10:03 +0000175 return Error(StartLoc, "expected relocatable expression");
Daniel Dunbar15d17072009-06-30 01:49:52 +0000176
177 return false;
178}
179
Daniel Dunbar2c3f00c2009-07-02 02:09:07 +0000180bool AsmParser::ParseParenRelocatableExpression(MCValue &Res) {
181 AsmExpr *Expr;
182
183 SMLoc StartLoc = Lexer.getLoc();
184 if (ParseParenExpr(Expr))
185 return true;
186
187 if (!Expr->EvaluateAsRelocatable(Ctx, Res))
188 return Error(StartLoc, "expected relocatable expression");
189
190 return false;
191}
192
Daniel Dunbar3f872332009-07-28 16:08:33 +0000193static unsigned getBinOpPrecedence(AsmToken::TokenKind K,
Daniel Dunbar475839e2009-06-29 20:37:27 +0000194 AsmBinaryExpr::Opcode &Kind) {
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000195 switch (K) {
196 default: return 0; // not a binop.
Daniel Dunbar475839e2009-06-29 20:37:27 +0000197
198 // Lowest Precedence: &&, ||
Daniel Dunbar3f872332009-07-28 16:08:33 +0000199 case AsmToken::AmpAmp:
Daniel Dunbar475839e2009-06-29 20:37:27 +0000200 Kind = AsmBinaryExpr::LAnd;
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000201 return 1;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000202 case AsmToken::PipePipe:
Daniel Dunbar475839e2009-06-29 20:37:27 +0000203 Kind = AsmBinaryExpr::LOr;
204 return 1;
205
206 // Low Precedence: +, -, ==, !=, <>, <, <=, >, >=
Daniel Dunbar3f872332009-07-28 16:08:33 +0000207 case AsmToken::Plus:
Daniel Dunbar475839e2009-06-29 20:37:27 +0000208 Kind = AsmBinaryExpr::Add;
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000209 return 2;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000210 case AsmToken::Minus:
Daniel Dunbar475839e2009-06-29 20:37:27 +0000211 Kind = AsmBinaryExpr::Sub;
212 return 2;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000213 case AsmToken::EqualEqual:
Daniel Dunbar475839e2009-06-29 20:37:27 +0000214 Kind = AsmBinaryExpr::EQ;
215 return 2;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000216 case AsmToken::ExclaimEqual:
217 case AsmToken::LessGreater:
Daniel Dunbar475839e2009-06-29 20:37:27 +0000218 Kind = AsmBinaryExpr::NE;
219 return 2;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000220 case AsmToken::Less:
Daniel Dunbar475839e2009-06-29 20:37:27 +0000221 Kind = AsmBinaryExpr::LT;
222 return 2;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000223 case AsmToken::LessEqual:
Daniel Dunbar475839e2009-06-29 20:37:27 +0000224 Kind = AsmBinaryExpr::LTE;
225 return 2;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000226 case AsmToken::Greater:
Daniel Dunbar475839e2009-06-29 20:37:27 +0000227 Kind = AsmBinaryExpr::GT;
228 return 2;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000229 case AsmToken::GreaterEqual:
Daniel Dunbar475839e2009-06-29 20:37:27 +0000230 Kind = AsmBinaryExpr::GTE;
231 return 2;
232
233 // Intermediate Precedence: |, &, ^
234 //
235 // FIXME: gas seems to support '!' as an infix operator?
Daniel Dunbar3f872332009-07-28 16:08:33 +0000236 case AsmToken::Pipe:
Daniel Dunbar475839e2009-06-29 20:37:27 +0000237 Kind = AsmBinaryExpr::Or;
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000238 return 3;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000239 case AsmToken::Caret:
Daniel Dunbar475839e2009-06-29 20:37:27 +0000240 Kind = AsmBinaryExpr::Xor;
241 return 3;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000242 case AsmToken::Amp:
Daniel Dunbar475839e2009-06-29 20:37:27 +0000243 Kind = AsmBinaryExpr::And;
244 return 3;
245
246 // Highest Precedence: *, /, %, <<, >>
Daniel Dunbar3f872332009-07-28 16:08:33 +0000247 case AsmToken::Star:
Daniel Dunbar475839e2009-06-29 20:37:27 +0000248 Kind = AsmBinaryExpr::Mul;
249 return 4;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000250 case AsmToken::Slash:
Daniel Dunbar475839e2009-06-29 20:37:27 +0000251 Kind = AsmBinaryExpr::Div;
252 return 4;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000253 case AsmToken::Percent:
Daniel Dunbar475839e2009-06-29 20:37:27 +0000254 Kind = AsmBinaryExpr::Mod;
255 return 4;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000256 case AsmToken::LessLess:
Daniel Dunbar475839e2009-06-29 20:37:27 +0000257 Kind = AsmBinaryExpr::Shl;
258 return 4;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000259 case AsmToken::GreaterGreater:
Daniel Dunbar475839e2009-06-29 20:37:27 +0000260 Kind = AsmBinaryExpr::Shr;
261 return 4;
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000262 }
263}
264
265
266/// ParseBinOpRHS - Parse all binary operators with precedence >= 'Precedence'.
267/// Res contains the LHS of the expression on input.
Daniel Dunbar475839e2009-06-29 20:37:27 +0000268bool AsmParser::ParseBinOpRHS(unsigned Precedence, AsmExpr *&Res) {
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000269 while (1) {
Daniel Dunbar51330632009-06-29 21:14:21 +0000270 AsmBinaryExpr::Opcode Kind = AsmBinaryExpr::Add;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000271 unsigned TokPrec = getBinOpPrecedence(Lexer.getKind(), Kind);
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000272
273 // If the next token is lower precedence than we are allowed to eat, return
274 // successfully with what we ate already.
275 if (TokPrec < Precedence)
276 return false;
277
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000278 Lexer.Lex();
279
280 // Eat the next primary expression.
Daniel Dunbar475839e2009-06-29 20:37:27 +0000281 AsmExpr *RHS;
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000282 if (ParsePrimaryExpr(RHS)) return true;
283
284 // If BinOp binds less tightly with RHS than the operator after RHS, let
285 // the pending operator take RHS as its LHS.
Daniel Dunbar475839e2009-06-29 20:37:27 +0000286 AsmBinaryExpr::Opcode Dummy;
287 unsigned NextTokPrec = getBinOpPrecedence(Lexer.getKind(), Dummy);
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000288 if (TokPrec < NextTokPrec) {
289 if (ParseBinOpRHS(Precedence+1, RHS)) return true;
290 }
291
Daniel Dunbar475839e2009-06-29 20:37:27 +0000292 // Merge LHS and RHS according to operator.
293 Res = new AsmBinaryExpr(Kind, Res, RHS);
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000294 }
295}
296
Chris Lattnerc4193832009-06-22 05:51:26 +0000297
298
299
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000300/// ParseStatement:
301/// ::= EndOfStatement
Chris Lattner2cf5f142009-06-22 01:29:09 +0000302/// ::= Label* Directive ...Operands... EndOfStatement
303/// ::= Label* Identifier OperandList* EndOfStatement
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000304bool AsmParser::ParseStatement() {
305 switch (Lexer.getKind()) {
306 default:
Chris Lattner14ee48a2009-06-21 21:22:11 +0000307 return TokError("unexpected token at start of statement");
Daniel Dunbar3f872332009-07-28 16:08:33 +0000308 case AsmToken::EndOfStatement:
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000309 Lexer.Lex();
310 return false;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000311 case AsmToken::Identifier:
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000312 break;
313 // TODO: Recurse on local labels etc.
314 }
315
316 // If we have an identifier, handle it as the key symbol.
Daniel Dunbar419aded2009-07-28 16:38:40 +0000317 AsmToken ID = Lexer.getTok();
318 SMLoc IDLoc = ID.getLoc();
319 StringRef IDVal = ID.getString();
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000320
321 // Consume the identifier, see what is after it.
Daniel Dunbara3c924f2009-07-28 16:56:42 +0000322 switch (Lexer.Lex().getKind()) {
Daniel Dunbar3f872332009-07-28 16:08:33 +0000323 case AsmToken::Colon: {
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000324 // identifier ':' -> Label.
325 Lexer.Lex();
Daniel Dunbardce0f3c2009-06-29 23:43:14 +0000326
327 // Diagnose attempt to use a variable as a label.
328 //
329 // FIXME: Diagnostics. Note the location of the definition as a label.
330 // FIXME: This doesn't diagnose assignment to a symbol which has been
331 // implicitly marked as external.
332 MCSymbol *Sym = Ctx.GetOrCreateSymbol(IDVal);
333 if (Sym->getSection())
334 return Error(IDLoc, "invalid symbol redefinition");
335 if (Ctx.GetSymbolValue(Sym))
336 return Error(IDLoc, "symbol already used as assembler variable");
Chris Lattnerc69485e2009-06-24 04:31:49 +0000337
338 // Since we saw a label, create a symbol and emit it.
339 // FIXME: If the label starts with L it is an assembler temporary label.
340 // Why does the client of this api need to know this?
Daniel Dunbardce0f3c2009-06-29 23:43:14 +0000341 Out.EmitLabel(Sym);
342
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000343 return ParseStatement();
Daniel Dunbardce0f3c2009-06-29 23:43:14 +0000344 }
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000345
Daniel Dunbar3f872332009-07-28 16:08:33 +0000346 case AsmToken::Equal:
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000347 // identifier '=' ... -> assignment statement
348 Lexer.Lex();
349
350 return ParseAssignment(IDVal, false);
351
352 default: // Normal instruction or directive.
353 break;
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000354 }
355
356 // Otherwise, we have a normal instruction or directive.
Chris Lattner2cf5f142009-06-22 01:29:09 +0000357 if (IDVal[0] == '.') {
Chris Lattner529fb542009-06-24 05:13:15 +0000358 // FIXME: This should be driven based on a hash lookup and callback.
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000359 if (IDVal == ".section")
Chris Lattner529fb542009-06-24 05:13:15 +0000360 return ParseDirectiveDarwinSection();
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000361 if (IDVal == ".text")
Chris Lattner529fb542009-06-24 05:13:15 +0000362 // FIXME: This changes behavior based on the -static flag to the
363 // assembler.
364 return ParseDirectiveSectionSwitch("__TEXT,__text",
365 "regular,pure_instructions");
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000366 if (IDVal == ".const")
Chris Lattner529fb542009-06-24 05:13:15 +0000367 return ParseDirectiveSectionSwitch("__TEXT,__const");
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000368 if (IDVal == ".static_const")
Chris Lattner529fb542009-06-24 05:13:15 +0000369 return ParseDirectiveSectionSwitch("__TEXT,__static_const");
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000370 if (IDVal == ".cstring")
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000371 return ParseDirectiveSectionSwitch("__TEXT,__cstring",
372 "cstring_literals");
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000373 if (IDVal == ".literal4")
Chris Lattner529fb542009-06-24 05:13:15 +0000374 return ParseDirectiveSectionSwitch("__TEXT,__literal4", "4byte_literals");
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000375 if (IDVal == ".literal8")
Chris Lattner529fb542009-06-24 05:13:15 +0000376 return ParseDirectiveSectionSwitch("__TEXT,__literal8", "8byte_literals");
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000377 if (IDVal == ".literal16")
Chris Lattner529fb542009-06-24 05:13:15 +0000378 return ParseDirectiveSectionSwitch("__TEXT,__literal16",
379 "16byte_literals");
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000380 if (IDVal == ".constructor")
Chris Lattner529fb542009-06-24 05:13:15 +0000381 return ParseDirectiveSectionSwitch("__TEXT,__constructor");
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000382 if (IDVal == ".destructor")
Chris Lattner529fb542009-06-24 05:13:15 +0000383 return ParseDirectiveSectionSwitch("__TEXT,__destructor");
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000384 if (IDVal == ".fvmlib_init0")
Chris Lattner529fb542009-06-24 05:13:15 +0000385 return ParseDirectiveSectionSwitch("__TEXT,__fvmlib_init0");
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000386 if (IDVal == ".fvmlib_init1")
Chris Lattner529fb542009-06-24 05:13:15 +0000387 return ParseDirectiveSectionSwitch("__TEXT,__fvmlib_init1");
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000388 if (IDVal == ".symbol_stub") // FIXME: Different on PPC.
Chris Lattner529fb542009-06-24 05:13:15 +0000389 return ParseDirectiveSectionSwitch("__IMPORT,__jump_table,symbol_stubs",
390 "self_modifying_code+pure_instructions,5");
391 // FIXME: .picsymbol_stub on PPC.
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000392 if (IDVal == ".data")
Chris Lattner529fb542009-06-24 05:13:15 +0000393 return ParseDirectiveSectionSwitch("__DATA,__data");
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000394 if (IDVal == ".static_data")
Chris Lattner529fb542009-06-24 05:13:15 +0000395 return ParseDirectiveSectionSwitch("__DATA,__static_data");
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000396 if (IDVal == ".non_lazy_symbol_pointer")
Chris Lattner529fb542009-06-24 05:13:15 +0000397 return ParseDirectiveSectionSwitch("__DATA,__nl_symbol_pointer",
398 "non_lazy_symbol_pointers");
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000399 if (IDVal == ".lazy_symbol_pointer")
Chris Lattner529fb542009-06-24 05:13:15 +0000400 return ParseDirectiveSectionSwitch("__DATA,__la_symbol_pointer",
401 "lazy_symbol_pointers");
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000402 if (IDVal == ".dyld")
Chris Lattner529fb542009-06-24 05:13:15 +0000403 return ParseDirectiveSectionSwitch("__DATA,__dyld");
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000404 if (IDVal == ".mod_init_func")
Chris Lattner529fb542009-06-24 05:13:15 +0000405 return ParseDirectiveSectionSwitch("__DATA,__mod_init_func",
406 "mod_init_funcs");
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000407 if (IDVal == ".mod_term_func")
Chris Lattner529fb542009-06-24 05:13:15 +0000408 return ParseDirectiveSectionSwitch("__DATA,__mod_term_func",
409 "mod_term_funcs");
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000410 if (IDVal == ".const_data")
Chris Lattner529fb542009-06-24 05:13:15 +0000411 return ParseDirectiveSectionSwitch("__DATA,__const", "regular");
412
413
414 // FIXME: Verify attributes on sections.
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000415 if (IDVal == ".objc_class")
Chris Lattner529fb542009-06-24 05:13:15 +0000416 return ParseDirectiveSectionSwitch("__OBJC,__class");
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000417 if (IDVal == ".objc_meta_class")
Chris Lattner529fb542009-06-24 05:13:15 +0000418 return ParseDirectiveSectionSwitch("__OBJC,__meta_class");
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000419 if (IDVal == ".objc_cat_cls_meth")
Chris Lattner529fb542009-06-24 05:13:15 +0000420 return ParseDirectiveSectionSwitch("__OBJC,__cat_cls_meth");
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000421 if (IDVal == ".objc_cat_inst_meth")
Chris Lattner529fb542009-06-24 05:13:15 +0000422 return ParseDirectiveSectionSwitch("__OBJC,__cat_inst_meth");
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000423 if (IDVal == ".objc_protocol")
Chris Lattner529fb542009-06-24 05:13:15 +0000424 return ParseDirectiveSectionSwitch("__OBJC,__protocol");
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000425 if (IDVal == ".objc_string_object")
Chris Lattner529fb542009-06-24 05:13:15 +0000426 return ParseDirectiveSectionSwitch("__OBJC,__string_object");
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000427 if (IDVal == ".objc_cls_meth")
Chris Lattner529fb542009-06-24 05:13:15 +0000428 return ParseDirectiveSectionSwitch("__OBJC,__cls_meth");
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000429 if (IDVal == ".objc_inst_meth")
Chris Lattner529fb542009-06-24 05:13:15 +0000430 return ParseDirectiveSectionSwitch("__OBJC,__inst_meth");
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000431 if (IDVal == ".objc_cls_refs")
Chris Lattner529fb542009-06-24 05:13:15 +0000432 return ParseDirectiveSectionSwitch("__OBJC,__cls_refs");
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000433 if (IDVal == ".objc_message_refs")
Chris Lattner529fb542009-06-24 05:13:15 +0000434 return ParseDirectiveSectionSwitch("__OBJC,__message_refs");
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000435 if (IDVal == ".objc_symbols")
Chris Lattner529fb542009-06-24 05:13:15 +0000436 return ParseDirectiveSectionSwitch("__OBJC,__symbols");
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000437 if (IDVal == ".objc_category")
Chris Lattner529fb542009-06-24 05:13:15 +0000438 return ParseDirectiveSectionSwitch("__OBJC,__category");
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000439 if (IDVal == ".objc_class_vars")
Chris Lattner529fb542009-06-24 05:13:15 +0000440 return ParseDirectiveSectionSwitch("__OBJC,__class_vars");
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000441 if (IDVal == ".objc_instance_vars")
Chris Lattner529fb542009-06-24 05:13:15 +0000442 return ParseDirectiveSectionSwitch("__OBJC,__instance_vars");
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000443 if (IDVal == ".objc_module_info")
Chris Lattner529fb542009-06-24 05:13:15 +0000444 return ParseDirectiveSectionSwitch("__OBJC,__module_info");
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000445 if (IDVal == ".objc_class_names")
Chris Lattner529fb542009-06-24 05:13:15 +0000446 return ParseDirectiveSectionSwitch("__TEXT,__cstring","cstring_literals");
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000447 if (IDVal == ".objc_meth_var_types")
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_names")
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_selector_strs")
Chris Lattner529fb542009-06-24 05:13:15 +0000452 return ParseDirectiveSectionSwitch("__OBJC,__selector_strs");
Chris Lattner9a023f72009-06-24 04:43:34 +0000453
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000454 // Assembler features
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000455 if (IDVal == ".set")
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000456 return ParseDirectiveSet();
457
Daniel Dunbara0d14262009-06-24 23:30:00 +0000458 // Data directives
459
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000460 if (IDVal == ".ascii")
Daniel Dunbara0d14262009-06-24 23:30:00 +0000461 return ParseDirectiveAscii(false);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000462 if (IDVal == ".asciz")
Daniel Dunbara0d14262009-06-24 23:30:00 +0000463 return ParseDirectiveAscii(true);
464
465 // FIXME: Target hooks for size? Also for "word", "hword".
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000466 if (IDVal == ".byte")
Daniel Dunbara0d14262009-06-24 23:30:00 +0000467 return ParseDirectiveValue(1);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000468 if (IDVal == ".short")
Daniel Dunbara0d14262009-06-24 23:30:00 +0000469 return ParseDirectiveValue(2);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000470 if (IDVal == ".long")
Daniel Dunbara0d14262009-06-24 23:30:00 +0000471 return ParseDirectiveValue(4);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000472 if (IDVal == ".quad")
Daniel Dunbara0d14262009-06-24 23:30:00 +0000473 return ParseDirectiveValue(8);
Daniel Dunbarc29dfa72009-06-29 23:46:59 +0000474
475 // FIXME: Target hooks for IsPow2.
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000476 if (IDVal == ".align")
Daniel Dunbarc29dfa72009-06-29 23:46:59 +0000477 return ParseDirectiveAlign(/*IsPow2=*/true, /*ExprSize=*/1);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000478 if (IDVal == ".align32")
Daniel Dunbarc29dfa72009-06-29 23:46:59 +0000479 return ParseDirectiveAlign(/*IsPow2=*/true, /*ExprSize=*/4);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000480 if (IDVal == ".balign")
Daniel Dunbarc29dfa72009-06-29 23:46:59 +0000481 return ParseDirectiveAlign(/*IsPow2=*/false, /*ExprSize=*/1);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000482 if (IDVal == ".balignw")
Daniel Dunbarc29dfa72009-06-29 23:46:59 +0000483 return ParseDirectiveAlign(/*IsPow2=*/false, /*ExprSize=*/2);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000484 if (IDVal == ".balignl")
Daniel Dunbarc29dfa72009-06-29 23:46:59 +0000485 return ParseDirectiveAlign(/*IsPow2=*/false, /*ExprSize=*/4);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000486 if (IDVal == ".p2align")
Daniel Dunbarc29dfa72009-06-29 23:46:59 +0000487 return ParseDirectiveAlign(/*IsPow2=*/true, /*ExprSize=*/1);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000488 if (IDVal == ".p2alignw")
Daniel Dunbarc29dfa72009-06-29 23:46:59 +0000489 return ParseDirectiveAlign(/*IsPow2=*/true, /*ExprSize=*/2);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000490 if (IDVal == ".p2alignl")
Daniel Dunbarc29dfa72009-06-29 23:46:59 +0000491 return ParseDirectiveAlign(/*IsPow2=*/true, /*ExprSize=*/4);
492
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000493 if (IDVal == ".org")
Daniel Dunbarc238b582009-06-25 22:44:51 +0000494 return ParseDirectiveOrg();
Daniel Dunbarc29dfa72009-06-29 23:46:59 +0000495
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000496 if (IDVal == ".fill")
Daniel Dunbarc29dfa72009-06-29 23:46:59 +0000497 return ParseDirectiveFill();
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000498 if (IDVal == ".space")
Daniel Dunbara0d14262009-06-24 23:30:00 +0000499 return ParseDirectiveSpace();
500
Daniel Dunbard7b267b2009-06-30 00:33:19 +0000501 // Symbol attribute directives
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000502 if (IDVal == ".globl" || IDVal == ".global")
Daniel Dunbard7b267b2009-06-30 00:33:19 +0000503 return ParseDirectiveSymbolAttribute(MCStreamer::Global);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000504 if (IDVal == ".hidden")
Daniel Dunbard7b267b2009-06-30 00:33:19 +0000505 return ParseDirectiveSymbolAttribute(MCStreamer::Hidden);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000506 if (IDVal == ".indirect_symbol")
Daniel Dunbard7b267b2009-06-30 00:33:19 +0000507 return ParseDirectiveSymbolAttribute(MCStreamer::IndirectSymbol);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000508 if (IDVal == ".internal")
Daniel Dunbard7b267b2009-06-30 00:33:19 +0000509 return ParseDirectiveSymbolAttribute(MCStreamer::Internal);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000510 if (IDVal == ".lazy_reference")
Daniel Dunbard7b267b2009-06-30 00:33:19 +0000511 return ParseDirectiveSymbolAttribute(MCStreamer::LazyReference);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000512 if (IDVal == ".no_dead_strip")
Daniel Dunbard7b267b2009-06-30 00:33:19 +0000513 return ParseDirectiveSymbolAttribute(MCStreamer::NoDeadStrip);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000514 if (IDVal == ".private_extern")
Daniel Dunbard7b267b2009-06-30 00:33:19 +0000515 return ParseDirectiveSymbolAttribute(MCStreamer::PrivateExtern);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000516 if (IDVal == ".protected")
Daniel Dunbard7b267b2009-06-30 00:33:19 +0000517 return ParseDirectiveSymbolAttribute(MCStreamer::Protected);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000518 if (IDVal == ".reference")
Daniel Dunbard7b267b2009-06-30 00:33:19 +0000519 return ParseDirectiveSymbolAttribute(MCStreamer::Reference);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000520 if (IDVal == ".weak")
Daniel Dunbard7b267b2009-06-30 00:33:19 +0000521 return ParseDirectiveSymbolAttribute(MCStreamer::Weak);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000522 if (IDVal == ".weak_definition")
Daniel Dunbard7b267b2009-06-30 00:33:19 +0000523 return ParseDirectiveSymbolAttribute(MCStreamer::WeakDefinition);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000524 if (IDVal == ".weak_reference")
Daniel Dunbard7b267b2009-06-30 00:33:19 +0000525 return ParseDirectiveSymbolAttribute(MCStreamer::WeakReference);
526
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000527 if (IDVal == ".comm")
Chris Lattner1fc3d752009-07-09 17:25:12 +0000528 return ParseDirectiveComm(/*IsLocal=*/false);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000529 if (IDVal == ".lcomm")
Chris Lattner1fc3d752009-07-09 17:25:12 +0000530 return ParseDirectiveComm(/*IsLocal=*/true);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000531 if (IDVal == ".zerofill")
Chris Lattner9be3fee2009-07-10 22:20:30 +0000532 return ParseDirectiveDarwinZerofill();
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000533 if (IDVal == ".desc")
Kevin Enderby95cf30c2009-07-14 18:17:10 +0000534 return ParseDirectiveDarwinSymbolDesc();
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000535 if (IDVal == ".lsym")
Kevin Enderby71148242009-07-14 21:35:03 +0000536 return ParseDirectiveDarwinLsym();
Chris Lattner4e4db7a2009-07-07 20:30:46 +0000537
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000538 if (IDVal == ".subsections_via_symbols")
Kevin Enderbya5c78322009-07-13 21:03:15 +0000539 return ParseDirectiveDarwinSubsectionsViaSymbols();
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000540 if (IDVal == ".abort")
Kevin Enderby5f1f0b82009-07-13 23:15:14 +0000541 return ParseDirectiveAbort();
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000542 if (IDVal == ".include")
Kevin Enderby1f049b22009-07-14 23:21:55 +0000543 return ParseDirectiveInclude();
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000544 if (IDVal == ".dump")
Kevin Enderby5026ae42009-07-20 20:25:37 +0000545 return ParseDirectiveDarwinDumpOrLoad(IDLoc, /*IsDump=*/true);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000546 if (IDVal == ".load")
Kevin Enderby5026ae42009-07-20 20:25:37 +0000547 return ParseDirectiveDarwinDumpOrLoad(IDLoc, /*IsLoad=*/false);
Kevin Enderbya5c78322009-07-13 21:03:15 +0000548
Daniel Dunbar3fb76832009-06-30 00:49:23 +0000549 Warning(IDLoc, "ignoring directive for now");
Chris Lattner2cf5f142009-06-22 01:29:09 +0000550 EatToEndOfStatement();
551 return false;
552 }
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000553
Chris Lattner29dfe7c2009-06-23 18:41:30 +0000554 MCInst Inst;
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000555 if (getTargetParser().ParseInstruction(IDVal, Inst))
Chris Lattner29dfe7c2009-06-23 18:41:30 +0000556 return true;
Chris Lattner2cf5f142009-06-22 01:29:09 +0000557
Daniel Dunbar3f872332009-07-28 16:08:33 +0000558 if (Lexer.isNot(AsmToken::EndOfStatement))
Chris Lattner9a023f72009-06-24 04:43:34 +0000559 return TokError("unexpected token in argument list");
Chris Lattner2cf5f142009-06-22 01:29:09 +0000560
561 // Eat the end of statement marker.
562 Lexer.Lex();
563
564 // Instruction is good, process it.
Daniel Dunbar0eebb052009-07-01 06:35:48 +0000565 Out.EmitInstruction(Inst);
Chris Lattner2cf5f142009-06-22 01:29:09 +0000566
567 // Skip to end of line for now.
Chris Lattner27aa7d22009-06-21 20:16:42 +0000568 return false;
569}
Chris Lattner9a023f72009-06-24 04:43:34 +0000570
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000571bool AsmParser::ParseAssignment(const StringRef &Name, bool IsDotSet) {
Daniel Dunbardce0f3c2009-06-29 23:43:14 +0000572 // FIXME: Use better location, we should use proper tokens.
573 SMLoc EqualLoc = Lexer.getLoc();
574
Daniel Dunbarf4b830f2009-06-30 02:10:03 +0000575 MCValue Value;
576 if (ParseRelocatableExpression(Value))
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000577 return true;
578
Daniel Dunbar3f872332009-07-28 16:08:33 +0000579 if (Lexer.isNot(AsmToken::EndOfStatement))
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000580 return TokError("unexpected token in assignment");
581
582 // Eat the end of statement marker.
583 Lexer.Lex();
584
Daniel Dunbardce0f3c2009-06-29 23:43:14 +0000585 // Diagnose assignment to a label.
586 //
587 // FIXME: Diagnostics. Note the location of the definition as a label.
588 // FIXME: This doesn't diagnose assignment to a symbol which has been
589 // implicitly marked as external.
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000590 // FIXME: Handle '.'.
Daniel Dunbardce0f3c2009-06-29 23:43:14 +0000591 // FIXME: Diagnose assignment to protected identifier (e.g., register name).
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000592 MCSymbol *Sym = Ctx.GetOrCreateSymbol(Name);
Daniel Dunbardce0f3c2009-06-29 23:43:14 +0000593 if (Sym->getSection())
594 return Error(EqualLoc, "invalid assignment to symbol emitted as a label");
595 if (Sym->isExternal())
596 return Error(EqualLoc, "invalid assignment to external symbol");
597
598 // Do the assignment.
Daniel Dunbarf4b830f2009-06-30 02:10:03 +0000599 Out.EmitAssignment(Sym, Value, IsDotSet);
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000600
601 return false;
602}
603
604/// ParseDirectiveSet:
605/// ::= .set identifier ',' expression
606bool AsmParser::ParseDirectiveSet() {
Daniel Dunbar3f872332009-07-28 16:08:33 +0000607 if (Lexer.isNot(AsmToken::Identifier))
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000608 return TokError("expected identifier after '.set' directive");
609
Daniel Dunbar419aded2009-07-28 16:38:40 +0000610 StringRef Name = Lexer.getTok().getString();
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000611
Daniel Dunbara3c924f2009-07-28 16:56:42 +0000612 if (Lexer.Lex().isNot(AsmToken::Comma))
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000613 return TokError("unexpected token in '.set'");
614 Lexer.Lex();
615
616 return ParseAssignment(Name, true);
617}
618
Chris Lattner9a023f72009-06-24 04:43:34 +0000619/// ParseDirectiveSection:
Chris Lattner529fb542009-06-24 05:13:15 +0000620/// ::= .section identifier (',' identifier)*
621/// FIXME: This should actually parse out the segment, section, attributes and
622/// sizeof_stub fields.
623bool AsmParser::ParseDirectiveDarwinSection() {
Daniel Dunbar3f872332009-07-28 16:08:33 +0000624 if (Lexer.isNot(AsmToken::Identifier))
Chris Lattner9a023f72009-06-24 04:43:34 +0000625 return TokError("expected identifier after '.section' directive");
626
Daniel Dunbar419aded2009-07-28 16:38:40 +0000627 std::string Section = Lexer.getTok().getString();
Chris Lattner9a023f72009-06-24 04:43:34 +0000628 Lexer.Lex();
629
630 // Accept a comma separated list of modifiers.
Daniel Dunbar3f872332009-07-28 16:08:33 +0000631 while (Lexer.is(AsmToken::Comma)) {
Chris Lattner9a023f72009-06-24 04:43:34 +0000632 Lexer.Lex();
633
Daniel Dunbar3f872332009-07-28 16:08:33 +0000634 if (Lexer.isNot(AsmToken::Identifier))
Chris Lattner9a023f72009-06-24 04:43:34 +0000635 return TokError("expected identifier in '.section' directive");
636 Section += ',';
Daniel Dunbar419aded2009-07-28 16:38:40 +0000637 Section += Lexer.getTok().getString().str();
Chris Lattner9a023f72009-06-24 04:43:34 +0000638 Lexer.Lex();
639 }
640
Daniel Dunbar3f872332009-07-28 16:08:33 +0000641 if (Lexer.isNot(AsmToken::EndOfStatement))
Chris Lattner9a023f72009-06-24 04:43:34 +0000642 return TokError("unexpected token in '.section' directive");
643 Lexer.Lex();
644
Chris Lattner56594f92009-07-31 17:47:16 +0000645 // FIXME: Arch specific.
646 MCSection *S = Ctx.GetSection(Section);
647 if (S == 0)
648 S = MCSection::Create(Section, Ctx);
649
650 Out.SwitchSection(S);
Chris Lattner9a023f72009-06-24 04:43:34 +0000651 return false;
652}
653
Chris Lattner529fb542009-06-24 05:13:15 +0000654bool AsmParser::ParseDirectiveSectionSwitch(const char *Section,
655 const char *Directives) {
Daniel Dunbar3f872332009-07-28 16:08:33 +0000656 if (Lexer.isNot(AsmToken::EndOfStatement))
Chris Lattner529fb542009-06-24 05:13:15 +0000657 return TokError("unexpected token in section switching directive");
658 Lexer.Lex();
659
660 std::string SectionStr = Section;
661 if (Directives && Directives[0]) {
662 SectionStr += ",";
663 SectionStr += Directives;
664 }
665
Chris Lattner56594f92009-07-31 17:47:16 +0000666 // FIXME: Arch specific.
667 MCSection *S = Ctx.GetSection(Section);
668 if (S == 0)
669 S = MCSection::Create(Section, Ctx);
670
671 Out.SwitchSection(S);
Chris Lattner529fb542009-06-24 05:13:15 +0000672 return false;
673}
Daniel Dunbara0d14262009-06-24 23:30:00 +0000674
675/// ParseDirectiveAscii:
Daniel Dunbar475839e2009-06-29 20:37:27 +0000676/// ::= ( .ascii | .asciz ) [ "string" ( , "string" )* ]
Daniel Dunbara0d14262009-06-24 23:30:00 +0000677bool AsmParser::ParseDirectiveAscii(bool ZeroTerminated) {
Daniel Dunbar3f872332009-07-28 16:08:33 +0000678 if (Lexer.isNot(AsmToken::EndOfStatement)) {
Daniel Dunbara0d14262009-06-24 23:30:00 +0000679 for (;;) {
Daniel Dunbar3f872332009-07-28 16:08:33 +0000680 if (Lexer.isNot(AsmToken::String))
Daniel Dunbara0d14262009-06-24 23:30:00 +0000681 return TokError("expected string in '.ascii' or '.asciz' directive");
682
683 // FIXME: This shouldn't use a const char* + strlen, the string could have
684 // embedded nulls.
685 // FIXME: Should have accessor for getting string contents.
Daniel Dunbar419aded2009-07-28 16:38:40 +0000686 StringRef Str = Lexer.getTok().getString();
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000687 Out.EmitBytes(Str.substr(1, Str.size() - 2));
Daniel Dunbara0d14262009-06-24 23:30:00 +0000688 if (ZeroTerminated)
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000689 Out.EmitBytes(StringRef("\0", 1));
Daniel Dunbara0d14262009-06-24 23:30:00 +0000690
691 Lexer.Lex();
692
Daniel Dunbar3f872332009-07-28 16:08:33 +0000693 if (Lexer.is(AsmToken::EndOfStatement))
Daniel Dunbara0d14262009-06-24 23:30:00 +0000694 break;
695
Daniel Dunbar3f872332009-07-28 16:08:33 +0000696 if (Lexer.isNot(AsmToken::Comma))
Daniel Dunbara0d14262009-06-24 23:30:00 +0000697 return TokError("unexpected token in '.ascii' or '.asciz' directive");
698 Lexer.Lex();
699 }
700 }
701
702 Lexer.Lex();
703 return false;
704}
705
706/// ParseDirectiveValue
707/// ::= (.byte | .short | ... ) [ expression (, expression)* ]
708bool AsmParser::ParseDirectiveValue(unsigned Size) {
Daniel Dunbar3f872332009-07-28 16:08:33 +0000709 if (Lexer.isNot(AsmToken::EndOfStatement)) {
Daniel Dunbara0d14262009-06-24 23:30:00 +0000710 for (;;) {
Daniel Dunbarf4b830f2009-06-30 02:10:03 +0000711 MCValue Expr;
712 if (ParseRelocatableExpression(Expr))
Daniel Dunbara0d14262009-06-24 23:30:00 +0000713 return true;
714
Daniel Dunbarf4b830f2009-06-30 02:10:03 +0000715 Out.EmitValue(Expr, Size);
Daniel Dunbara0d14262009-06-24 23:30:00 +0000716
Daniel Dunbar3f872332009-07-28 16:08:33 +0000717 if (Lexer.is(AsmToken::EndOfStatement))
Daniel Dunbara0d14262009-06-24 23:30:00 +0000718 break;
719
720 // FIXME: Improve diagnostic.
Daniel Dunbar3f872332009-07-28 16:08:33 +0000721 if (Lexer.isNot(AsmToken::Comma))
Daniel Dunbara0d14262009-06-24 23:30:00 +0000722 return TokError("unexpected token in directive");
723 Lexer.Lex();
724 }
725 }
726
727 Lexer.Lex();
728 return false;
729}
730
731/// ParseDirectiveSpace
732/// ::= .space expression [ , expression ]
733bool AsmParser::ParseDirectiveSpace() {
734 int64_t NumBytes;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000735 if (ParseAbsoluteExpression(NumBytes))
Daniel Dunbara0d14262009-06-24 23:30:00 +0000736 return true;
737
738 int64_t FillExpr = 0;
739 bool HasFillExpr = false;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000740 if (Lexer.isNot(AsmToken::EndOfStatement)) {
741 if (Lexer.isNot(AsmToken::Comma))
Daniel Dunbara0d14262009-06-24 23:30:00 +0000742 return TokError("unexpected token in '.space' directive");
743 Lexer.Lex();
744
Daniel Dunbar475839e2009-06-29 20:37:27 +0000745 if (ParseAbsoluteExpression(FillExpr))
Daniel Dunbara0d14262009-06-24 23:30:00 +0000746 return true;
747
748 HasFillExpr = true;
749
Daniel Dunbar3f872332009-07-28 16:08:33 +0000750 if (Lexer.isNot(AsmToken::EndOfStatement))
Daniel Dunbara0d14262009-06-24 23:30:00 +0000751 return TokError("unexpected token in '.space' directive");
752 }
753
754 Lexer.Lex();
755
756 if (NumBytes <= 0)
757 return TokError("invalid number of bytes in '.space' directive");
758
759 // FIXME: Sometimes the fill expr is 'nop' if it isn't supplied, instead of 0.
760 for (uint64_t i = 0, e = NumBytes; i != e; ++i)
761 Out.EmitValue(MCValue::get(FillExpr), 1);
762
763 return false;
764}
765
766/// ParseDirectiveFill
767/// ::= .fill expression , expression , expression
768bool AsmParser::ParseDirectiveFill() {
769 int64_t NumValues;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000770 if (ParseAbsoluteExpression(NumValues))
Daniel Dunbara0d14262009-06-24 23:30:00 +0000771 return true;
772
Daniel Dunbar3f872332009-07-28 16:08:33 +0000773 if (Lexer.isNot(AsmToken::Comma))
Daniel Dunbara0d14262009-06-24 23:30:00 +0000774 return TokError("unexpected token in '.fill' directive");
775 Lexer.Lex();
776
777 int64_t FillSize;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000778 if (ParseAbsoluteExpression(FillSize))
Daniel Dunbara0d14262009-06-24 23:30:00 +0000779 return true;
780
Daniel Dunbar3f872332009-07-28 16:08:33 +0000781 if (Lexer.isNot(AsmToken::Comma))
Daniel Dunbara0d14262009-06-24 23:30:00 +0000782 return TokError("unexpected token in '.fill' directive");
783 Lexer.Lex();
784
785 int64_t FillExpr;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000786 if (ParseAbsoluteExpression(FillExpr))
Daniel Dunbara0d14262009-06-24 23:30:00 +0000787 return true;
788
Daniel Dunbar3f872332009-07-28 16:08:33 +0000789 if (Lexer.isNot(AsmToken::EndOfStatement))
Daniel Dunbara0d14262009-06-24 23:30:00 +0000790 return TokError("unexpected token in '.fill' directive");
791
792 Lexer.Lex();
793
794 if (FillSize != 1 && FillSize != 2 && FillSize != 4)
795 return TokError("invalid '.fill' size, expected 1, 2, or 4");
796
797 for (uint64_t i = 0, e = NumValues; i != e; ++i)
798 Out.EmitValue(MCValue::get(FillExpr), FillSize);
799
800 return false;
801}
Daniel Dunbarc238b582009-06-25 22:44:51 +0000802
803/// ParseDirectiveOrg
804/// ::= .org expression [ , expression ]
805bool AsmParser::ParseDirectiveOrg() {
Daniel Dunbarf4b830f2009-06-30 02:10:03 +0000806 MCValue Offset;
807 if (ParseRelocatableExpression(Offset))
Daniel Dunbarc238b582009-06-25 22:44:51 +0000808 return true;
809
810 // Parse optional fill expression.
811 int64_t FillExpr = 0;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000812 if (Lexer.isNot(AsmToken::EndOfStatement)) {
813 if (Lexer.isNot(AsmToken::Comma))
Daniel Dunbarc238b582009-06-25 22:44:51 +0000814 return TokError("unexpected token in '.org' directive");
815 Lexer.Lex();
816
Daniel Dunbar475839e2009-06-29 20:37:27 +0000817 if (ParseAbsoluteExpression(FillExpr))
Daniel Dunbarc238b582009-06-25 22:44:51 +0000818 return true;
819
Daniel Dunbar3f872332009-07-28 16:08:33 +0000820 if (Lexer.isNot(AsmToken::EndOfStatement))
Daniel Dunbarc238b582009-06-25 22:44:51 +0000821 return TokError("unexpected token in '.org' directive");
822 }
823
824 Lexer.Lex();
Daniel Dunbarf4b830f2009-06-30 02:10:03 +0000825
826 // FIXME: Only limited forms of relocatable expressions are accepted here, it
827 // has to be relative to the current section.
828 Out.EmitValueToOffset(Offset, FillExpr);
Daniel Dunbarc238b582009-06-25 22:44:51 +0000829
830 return false;
831}
Daniel Dunbarc29dfa72009-06-29 23:46:59 +0000832
833/// ParseDirectiveAlign
834/// ::= {.align, ...} expression [ , expression [ , expression ]]
835bool AsmParser::ParseDirectiveAlign(bool IsPow2, unsigned ValueSize) {
836 int64_t Alignment;
837 if (ParseAbsoluteExpression(Alignment))
838 return true;
839
840 SMLoc MaxBytesLoc;
841 bool HasFillExpr = false;
842 int64_t FillExpr = 0;
843 int64_t MaxBytesToFill = 0;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000844 if (Lexer.isNot(AsmToken::EndOfStatement)) {
845 if (Lexer.isNot(AsmToken::Comma))
Daniel Dunbarc29dfa72009-06-29 23:46:59 +0000846 return TokError("unexpected token in directive");
847 Lexer.Lex();
848
849 // The fill expression can be omitted while specifying a maximum number of
850 // alignment bytes, e.g:
851 // .align 3,,4
Daniel Dunbar3f872332009-07-28 16:08:33 +0000852 if (Lexer.isNot(AsmToken::Comma)) {
Daniel Dunbarc29dfa72009-06-29 23:46:59 +0000853 HasFillExpr = true;
854 if (ParseAbsoluteExpression(FillExpr))
855 return true;
856 }
857
Daniel Dunbar3f872332009-07-28 16:08:33 +0000858 if (Lexer.isNot(AsmToken::EndOfStatement)) {
859 if (Lexer.isNot(AsmToken::Comma))
Daniel Dunbarc29dfa72009-06-29 23:46:59 +0000860 return TokError("unexpected token in directive");
861 Lexer.Lex();
862
863 MaxBytesLoc = Lexer.getLoc();
864 if (ParseAbsoluteExpression(MaxBytesToFill))
865 return true;
866
Daniel Dunbar3f872332009-07-28 16:08:33 +0000867 if (Lexer.isNot(AsmToken::EndOfStatement))
Daniel Dunbarc29dfa72009-06-29 23:46:59 +0000868 return TokError("unexpected token in directive");
869 }
870 }
871
872 Lexer.Lex();
873
874 if (!HasFillExpr) {
875 // FIXME: Sometimes fill with nop.
876 FillExpr = 0;
877 }
878
879 // Compute alignment in bytes.
880 if (IsPow2) {
881 // FIXME: Diagnose overflow.
Chris Lattner39750252009-07-11 22:32:37 +0000882 Alignment = 1LL << Alignment;
Daniel Dunbarc29dfa72009-06-29 23:46:59 +0000883 }
884
885 // Diagnose non-sensical max bytes to fill.
886 if (MaxBytesLoc.isValid()) {
887 if (MaxBytesToFill < 1) {
Daniel Dunbar3fb76832009-06-30 00:49:23 +0000888 Warning(MaxBytesLoc, "alignment directive can never be satisfied in this "
889 "many bytes, ignoring");
Daniel Dunbarc29dfa72009-06-29 23:46:59 +0000890 return false;
891 }
892
893 if (MaxBytesToFill >= Alignment) {
Daniel Dunbar3fb76832009-06-30 00:49:23 +0000894 Warning(MaxBytesLoc, "maximum bytes expression exceeds alignment and "
895 "has no effect");
Daniel Dunbarc29dfa72009-06-29 23:46:59 +0000896 MaxBytesToFill = 0;
897 }
898 }
899
900 // FIXME: Target specific behavior about how the "extra" bytes are filled.
901 Out.EmitValueToAlignment(Alignment, FillExpr, ValueSize, MaxBytesToFill);
902
903 return false;
904}
905
Daniel Dunbard7b267b2009-06-30 00:33:19 +0000906/// ParseDirectiveSymbolAttribute
907/// ::= { ".globl", ".weak", ... } [ identifier ( , identifier )* ]
908bool AsmParser::ParseDirectiveSymbolAttribute(MCStreamer::SymbolAttr Attr) {
Daniel Dunbar3f872332009-07-28 16:08:33 +0000909 if (Lexer.isNot(AsmToken::EndOfStatement)) {
Daniel Dunbard7b267b2009-06-30 00:33:19 +0000910 for (;;) {
Daniel Dunbar3f872332009-07-28 16:08:33 +0000911 if (Lexer.isNot(AsmToken::Identifier))
Daniel Dunbard7b267b2009-06-30 00:33:19 +0000912 return TokError("expected identifier in directive");
913
Daniel Dunbar419aded2009-07-28 16:38:40 +0000914 MCSymbol *Sym = Ctx.GetOrCreateSymbol(Lexer.getTok().getString());
Daniel Dunbard7b267b2009-06-30 00:33:19 +0000915 Lexer.Lex();
916
917 // If this is use of an undefined symbol then mark it external.
918 if (!Sym->getSection() && !Ctx.GetSymbolValue(Sym))
919 Sym->setExternal(true);
920
921 Out.EmitSymbolAttribute(Sym, Attr);
922
Daniel Dunbar3f872332009-07-28 16:08:33 +0000923 if (Lexer.is(AsmToken::EndOfStatement))
Daniel Dunbard7b267b2009-06-30 00:33:19 +0000924 break;
925
Daniel Dunbar3f872332009-07-28 16:08:33 +0000926 if (Lexer.isNot(AsmToken::Comma))
Daniel Dunbard7b267b2009-06-30 00:33:19 +0000927 return TokError("unexpected token in directive");
928 Lexer.Lex();
929 }
930 }
931
932 Lexer.Lex();
933 return false;
934}
Chris Lattner4e4db7a2009-07-07 20:30:46 +0000935
Kevin Enderby95cf30c2009-07-14 18:17:10 +0000936/// ParseDirectiveDarwinSymbolDesc
937/// ::= .desc identifier , expression
938bool AsmParser::ParseDirectiveDarwinSymbolDesc() {
Daniel Dunbar3f872332009-07-28 16:08:33 +0000939 if (Lexer.isNot(AsmToken::Identifier))
Kevin Enderby95cf30c2009-07-14 18:17:10 +0000940 return TokError("expected identifier in directive");
941
942 // handle the identifier as the key symbol.
943 SMLoc IDLoc = Lexer.getLoc();
Daniel Dunbar419aded2009-07-28 16:38:40 +0000944 MCSymbol *Sym = Ctx.GetOrCreateSymbol(Lexer.getTok().getString());
Kevin Enderby95cf30c2009-07-14 18:17:10 +0000945 Lexer.Lex();
946
Daniel Dunbar3f872332009-07-28 16:08:33 +0000947 if (Lexer.isNot(AsmToken::Comma))
Kevin Enderby95cf30c2009-07-14 18:17:10 +0000948 return TokError("unexpected token in '.desc' directive");
949 Lexer.Lex();
950
951 SMLoc DescLoc = Lexer.getLoc();
952 int64_t DescValue;
953 if (ParseAbsoluteExpression(DescValue))
954 return true;
955
Daniel Dunbar3f872332009-07-28 16:08:33 +0000956 if (Lexer.isNot(AsmToken::EndOfStatement))
Kevin Enderby95cf30c2009-07-14 18:17:10 +0000957 return TokError("unexpected token in '.desc' directive");
958
959 Lexer.Lex();
960
961 // Set the n_desc field of this Symbol to this DescValue
962 Out.EmitSymbolDesc(Sym, DescValue);
963
964 return false;
965}
966
Chris Lattner4e4db7a2009-07-07 20:30:46 +0000967/// ParseDirectiveComm
Chris Lattner1fc3d752009-07-09 17:25:12 +0000968/// ::= ( .comm | .lcomm ) identifier , size_expression [ , align_expression ]
969bool AsmParser::ParseDirectiveComm(bool IsLocal) {
Daniel Dunbar3f872332009-07-28 16:08:33 +0000970 if (Lexer.isNot(AsmToken::Identifier))
Chris Lattner4e4db7a2009-07-07 20:30:46 +0000971 return TokError("expected identifier in directive");
972
973 // handle the identifier as the key symbol.
974 SMLoc IDLoc = Lexer.getLoc();
Daniel Dunbar419aded2009-07-28 16:38:40 +0000975 MCSymbol *Sym = Ctx.GetOrCreateSymbol(Lexer.getTok().getString());
Chris Lattner4e4db7a2009-07-07 20:30:46 +0000976 Lexer.Lex();
977
Daniel Dunbar3f872332009-07-28 16:08:33 +0000978 if (Lexer.isNot(AsmToken::Comma))
Chris Lattner4e4db7a2009-07-07 20:30:46 +0000979 return TokError("unexpected token in directive");
980 Lexer.Lex();
981
982 int64_t Size;
983 SMLoc SizeLoc = Lexer.getLoc();
984 if (ParseAbsoluteExpression(Size))
985 return true;
986
987 int64_t Pow2Alignment = 0;
988 SMLoc Pow2AlignmentLoc;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000989 if (Lexer.is(AsmToken::Comma)) {
Chris Lattner4e4db7a2009-07-07 20:30:46 +0000990 Lexer.Lex();
991 Pow2AlignmentLoc = Lexer.getLoc();
992 if (ParseAbsoluteExpression(Pow2Alignment))
993 return true;
994 }
995
Daniel Dunbar3f872332009-07-28 16:08:33 +0000996 if (Lexer.isNot(AsmToken::EndOfStatement))
Chris Lattner1fc3d752009-07-09 17:25:12 +0000997 return TokError("unexpected token in '.comm' or '.lcomm' directive");
Chris Lattner4e4db7a2009-07-07 20:30:46 +0000998
999 Lexer.Lex();
1000
Chris Lattner1fc3d752009-07-09 17:25:12 +00001001 // NOTE: a size of zero for a .comm should create a undefined symbol
1002 // but a size of .lcomm creates a bss symbol of size zero.
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001003 if (Size < 0)
Chris Lattner1fc3d752009-07-09 17:25:12 +00001004 return Error(SizeLoc, "invalid '.comm' or '.lcomm' directive size, can't "
1005 "be less than zero");
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001006
1007 // NOTE: The alignment in the directive is a power of 2 value, the assember
1008 // may internally end up wanting an alignment in bytes.
1009 // FIXME: Diagnose overflow.
1010 if (Pow2Alignment < 0)
Chris Lattner1fc3d752009-07-09 17:25:12 +00001011 return Error(Pow2AlignmentLoc, "invalid '.comm' or '.lcomm' directive "
1012 "alignment, can't be less than zero");
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001013
1014 // TODO: Symbol must be undefined or it is a error to re-defined the symbol
1015 if (Sym->getSection() || Ctx.GetSymbolValue(Sym))
1016 return Error(IDLoc, "invalid symbol redefinition");
1017
Chris Lattner1fc3d752009-07-09 17:25:12 +00001018 // Create the Symbol as a common or local common with Size and Pow2Alignment
1019 Out.EmitCommonSymbol(Sym, Size, Pow2Alignment, IsLocal);
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001020
1021 return false;
1022}
Chris Lattner9be3fee2009-07-10 22:20:30 +00001023
1024/// ParseDirectiveDarwinZerofill
1025/// ::= .zerofill segname , sectname [, identifier , size_expression [
1026/// , align_expression ]]
1027bool AsmParser::ParseDirectiveDarwinZerofill() {
Daniel Dunbar3f872332009-07-28 16:08:33 +00001028 if (Lexer.isNot(AsmToken::Identifier))
Chris Lattner9be3fee2009-07-10 22:20:30 +00001029 return TokError("expected segment name after '.zerofill' directive");
Daniel Dunbar419aded2009-07-28 16:38:40 +00001030 std::string Section = Lexer.getTok().getString();
Chris Lattner9be3fee2009-07-10 22:20:30 +00001031 Lexer.Lex();
1032
Daniel Dunbar3f872332009-07-28 16:08:33 +00001033 if (Lexer.isNot(AsmToken::Comma))
Chris Lattner9be3fee2009-07-10 22:20:30 +00001034 return TokError("unexpected token in directive");
1035 Section += ',';
1036 Lexer.Lex();
1037
Daniel Dunbar3f872332009-07-28 16:08:33 +00001038 if (Lexer.isNot(AsmToken::Identifier))
Chris Lattner9be3fee2009-07-10 22:20:30 +00001039 return TokError("expected section name after comma in '.zerofill' "
1040 "directive");
Daniel Dunbar419aded2009-07-28 16:38:40 +00001041 Section += Lexer.getTok().getString().str();
Chris Lattner9be3fee2009-07-10 22:20:30 +00001042 Lexer.Lex();
1043
1044 // FIXME: we will need to tell GetSection() that this is to be created with or
1045 // must have the Mach-O section type of S_ZEROFILL. Something like the code
1046 // below could be done but for now it is not as EmitZerofill() does not know
1047 // how to deal with a section type in the section name like
1048 // ParseDirectiveDarwinSection() allows.
1049 // Section += ',';
1050 // Section += "zerofill";
1051
1052 // If this is the end of the line all that was wanted was to create the
1053 // the section but with no symbol.
Daniel Dunbar3f872332009-07-28 16:08:33 +00001054 if (Lexer.is(AsmToken::EndOfStatement)) {
Chris Lattner9be3fee2009-07-10 22:20:30 +00001055 // Create the zerofill section but no symbol
1056 Out.EmitZerofill(Ctx.GetSection(Section.c_str()));
1057 return false;
1058 }
1059
Daniel Dunbar3f872332009-07-28 16:08:33 +00001060 if (Lexer.isNot(AsmToken::Comma))
Chris Lattner9be3fee2009-07-10 22:20:30 +00001061 return TokError("unexpected token in directive");
1062 Lexer.Lex();
1063
Daniel Dunbar3f872332009-07-28 16:08:33 +00001064 if (Lexer.isNot(AsmToken::Identifier))
Chris Lattner9be3fee2009-07-10 22:20:30 +00001065 return TokError("expected identifier in directive");
1066
1067 // handle the identifier as the key symbol.
1068 SMLoc IDLoc = Lexer.getLoc();
Daniel Dunbar419aded2009-07-28 16:38:40 +00001069 MCSymbol *Sym = Ctx.GetOrCreateSymbol(Lexer.getTok().getString());
Chris Lattner9be3fee2009-07-10 22:20:30 +00001070 Lexer.Lex();
1071
Daniel Dunbar3f872332009-07-28 16:08:33 +00001072 if (Lexer.isNot(AsmToken::Comma))
Chris Lattner9be3fee2009-07-10 22:20:30 +00001073 return TokError("unexpected token in directive");
1074 Lexer.Lex();
1075
1076 int64_t Size;
1077 SMLoc SizeLoc = Lexer.getLoc();
1078 if (ParseAbsoluteExpression(Size))
1079 return true;
1080
1081 int64_t Pow2Alignment = 0;
1082 SMLoc Pow2AlignmentLoc;
Daniel Dunbar3f872332009-07-28 16:08:33 +00001083 if (Lexer.is(AsmToken::Comma)) {
Chris Lattner9be3fee2009-07-10 22:20:30 +00001084 Lexer.Lex();
1085 Pow2AlignmentLoc = Lexer.getLoc();
1086 if (ParseAbsoluteExpression(Pow2Alignment))
1087 return true;
1088 }
1089
Daniel Dunbar3f872332009-07-28 16:08:33 +00001090 if (Lexer.isNot(AsmToken::EndOfStatement))
Chris Lattner9be3fee2009-07-10 22:20:30 +00001091 return TokError("unexpected token in '.zerofill' directive");
1092
1093 Lexer.Lex();
1094
1095 if (Size < 0)
1096 return Error(SizeLoc, "invalid '.zerofill' directive size, can't be less "
1097 "than zero");
1098
1099 // NOTE: The alignment in the directive is a power of 2 value, the assember
1100 // may internally end up wanting an alignment in bytes.
1101 // FIXME: Diagnose overflow.
1102 if (Pow2Alignment < 0)
1103 return Error(Pow2AlignmentLoc, "invalid '.zerofill' directive alignment, "
1104 "can't be less than zero");
1105
1106 // TODO: Symbol must be undefined or it is a error to re-defined the symbol
1107 if (Sym->getSection() || Ctx.GetSymbolValue(Sym))
1108 return Error(IDLoc, "invalid symbol redefinition");
1109
1110 // Create the zerofill Symbol with Size and Pow2Alignment
1111 Out.EmitZerofill(Ctx.GetSection(Section.c_str()), Sym, Size, Pow2Alignment);
1112
1113 return false;
1114}
Kevin Enderbya5c78322009-07-13 21:03:15 +00001115
1116/// ParseDirectiveDarwinSubsectionsViaSymbols
1117/// ::= .subsections_via_symbols
1118bool AsmParser::ParseDirectiveDarwinSubsectionsViaSymbols() {
Daniel Dunbar3f872332009-07-28 16:08:33 +00001119 if (Lexer.isNot(AsmToken::EndOfStatement))
Kevin Enderbya5c78322009-07-13 21:03:15 +00001120 return TokError("unexpected token in '.subsections_via_symbols' directive");
1121
1122 Lexer.Lex();
1123
Kevin Enderbyf96db462009-07-16 17:56:39 +00001124 Out.EmitAssemblerFlag(MCStreamer::SubsectionsViaSymbols);
Kevin Enderbya5c78322009-07-13 21:03:15 +00001125
1126 return false;
1127}
Kevin Enderby5f1f0b82009-07-13 23:15:14 +00001128
1129/// ParseDirectiveAbort
1130/// ::= .abort [ "abort_string" ]
1131bool AsmParser::ParseDirectiveAbort() {
Daniel Dunbarf9507ff2009-07-27 23:20:52 +00001132 // FIXME: Use loc from directive.
1133 SMLoc Loc = Lexer.getLoc();
1134
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001135 StringRef Str = "";
Daniel Dunbar3f872332009-07-28 16:08:33 +00001136 if (Lexer.isNot(AsmToken::EndOfStatement)) {
1137 if (Lexer.isNot(AsmToken::String))
Kevin Enderby5f1f0b82009-07-13 23:15:14 +00001138 return TokError("expected string in '.abort' directive");
1139
Daniel Dunbar419aded2009-07-28 16:38:40 +00001140 Str = Lexer.getTok().getString();
Kevin Enderby5f1f0b82009-07-13 23:15:14 +00001141
1142 Lexer.Lex();
1143 }
1144
Daniel Dunbar3f872332009-07-28 16:08:33 +00001145 if (Lexer.isNot(AsmToken::EndOfStatement))
Kevin Enderby5f1f0b82009-07-13 23:15:14 +00001146 return TokError("unexpected token in '.abort' directive");
1147
1148 Lexer.Lex();
1149
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001150 // FIXME: Handle here.
Daniel Dunbarf9507ff2009-07-27 23:20:52 +00001151 if (Str.empty())
1152 Error(Loc, ".abort detected. Assembly stopping.");
1153 else
1154 Error(Loc, ".abort '" + Str + "' detected. Assembly stopping.");
Kevin Enderby5f1f0b82009-07-13 23:15:14 +00001155
1156 return false;
1157}
Kevin Enderby71148242009-07-14 21:35:03 +00001158
1159/// ParseDirectiveLsym
1160/// ::= .lsym identifier , expression
1161bool AsmParser::ParseDirectiveDarwinLsym() {
Daniel Dunbar3f872332009-07-28 16:08:33 +00001162 if (Lexer.isNot(AsmToken::Identifier))
Kevin Enderby71148242009-07-14 21:35:03 +00001163 return TokError("expected identifier in directive");
1164
1165 // handle the identifier as the key symbol.
1166 SMLoc IDLoc = Lexer.getLoc();
Daniel Dunbar419aded2009-07-28 16:38:40 +00001167 MCSymbol *Sym = Ctx.GetOrCreateSymbol(Lexer.getTok().getString());
Kevin Enderby71148242009-07-14 21:35:03 +00001168 Lexer.Lex();
1169
Daniel Dunbar3f872332009-07-28 16:08:33 +00001170 if (Lexer.isNot(AsmToken::Comma))
Kevin Enderby71148242009-07-14 21:35:03 +00001171 return TokError("unexpected token in '.lsym' directive");
1172 Lexer.Lex();
1173
1174 MCValue Expr;
1175 if (ParseRelocatableExpression(Expr))
1176 return true;
1177
Daniel Dunbar3f872332009-07-28 16:08:33 +00001178 if (Lexer.isNot(AsmToken::EndOfStatement))
Kevin Enderby71148242009-07-14 21:35:03 +00001179 return TokError("unexpected token in '.lsym' directive");
1180
1181 Lexer.Lex();
1182
1183 // Create the Sym with the value of the Expr
1184 Out.EmitLocalSymbol(Sym, Expr);
1185
1186 return false;
1187}
Kevin Enderby1f049b22009-07-14 23:21:55 +00001188
1189/// ParseDirectiveInclude
1190/// ::= .include "filename"
1191bool AsmParser::ParseDirectiveInclude() {
Daniel Dunbar3f872332009-07-28 16:08:33 +00001192 if (Lexer.isNot(AsmToken::String))
Kevin Enderby1f049b22009-07-14 23:21:55 +00001193 return TokError("expected string in '.include' directive");
1194
Daniel Dunbar419aded2009-07-28 16:38:40 +00001195 std::string Filename = Lexer.getTok().getString();
Chris Lattner8e25e2d2009-07-16 06:14:39 +00001196 SMLoc IncludeLoc = Lexer.getLoc();
Kevin Enderby1f049b22009-07-14 23:21:55 +00001197 Lexer.Lex();
1198
Daniel Dunbar3f872332009-07-28 16:08:33 +00001199 if (Lexer.isNot(AsmToken::EndOfStatement))
Kevin Enderby1f049b22009-07-14 23:21:55 +00001200 return TokError("unexpected token in '.include' directive");
1201
Chris Lattner8e25e2d2009-07-16 06:14:39 +00001202 // Strip the quotes.
1203 Filename = Filename.substr(1, Filename.size()-2);
1204
1205 // Attempt to switch the lexer to the included file before consuming the end
1206 // of statement to avoid losing it when we switch.
1207 if (Lexer.EnterIncludeFile(Filename)) {
1208 Lexer.PrintMessage(IncludeLoc,
1209 "Could not find include file '" + Filename + "'",
1210 "error");
1211 return true;
1212 }
Kevin Enderby1f049b22009-07-14 23:21:55 +00001213
1214 return false;
1215}
Kevin Enderby6e68cd92009-07-15 15:30:11 +00001216
1217/// ParseDirectiveDarwinDumpOrLoad
1218/// ::= ( .dump | .load ) "filename"
Kevin Enderby5026ae42009-07-20 20:25:37 +00001219bool AsmParser::ParseDirectiveDarwinDumpOrLoad(SMLoc IDLoc, bool IsDump) {
Daniel Dunbar3f872332009-07-28 16:08:33 +00001220 if (Lexer.isNot(AsmToken::String))
Kevin Enderby6e68cd92009-07-15 15:30:11 +00001221 return TokError("expected string in '.dump' or '.load' directive");
1222
Kevin Enderby6e68cd92009-07-15 15:30:11 +00001223 Lexer.Lex();
1224
Daniel Dunbar3f872332009-07-28 16:08:33 +00001225 if (Lexer.isNot(AsmToken::EndOfStatement))
Kevin Enderby6e68cd92009-07-15 15:30:11 +00001226 return TokError("unexpected token in '.dump' or '.load' directive");
1227
1228 Lexer.Lex();
1229
Kevin Enderby5026ae42009-07-20 20:25:37 +00001230 // FIXME: If/when .dump and .load are implemented they will be done in the
1231 // the assembly parser and not have any need for an MCStreamer API.
Kevin Enderby6e68cd92009-07-15 15:30:11 +00001232 if (IsDump)
Kevin Enderby5026ae42009-07-20 20:25:37 +00001233 Warning(IDLoc, "ignoring directive .dump for now");
Kevin Enderby6e68cd92009-07-15 15:30:11 +00001234 else
Kevin Enderby5026ae42009-07-20 20:25:37 +00001235 Warning(IDLoc, "ignoring directive .load for now");
Kevin Enderby6e68cd92009-07-15 15:30:11 +00001236
1237 return false;
1238}